The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> System Administration
> FTP Help
|
ftp upload ware
Discuss ftp upload ware in the FTP Help forum on Dev Shed. ftp upload ware FTP Help forum discussing FTP practices, tips and solutions for problems with FTP on multiple platforms. File Transfer Protocol (FTP) was designed specifically for transferring files from one machine to another.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 12th, 2002, 05:44 PM
|
|
Contributing User
|
|
Join Date: Aug 2002
Posts: 110
Time spent in forums: 6 h 16 m 17 sec
Reputation Power: 11
|
|
|
ftp upload ware
I have a working php ftp upload modual that works perfectlly for uploading '.txt' files. I have been trying to get this to upload pictures instead, but I can not seem to get it to work properly. What happens is I get the picture icon but with no data inside the picture.
This is the script that will work properly for txt files, does anybody now how to modifiy this to upload 'gif', 'jpeg', 'png'? Thanks in advance for anybody that could help me out. This modual is open source, please feel free to use.
This is broken into two pages for clarity.
//html for that links to the php upload page
<html>
<head>
<title>Administration - upload new files</title>
</head>
<body>
<h1>Upload new news files</h1>
<form enctype="multipart/form-data" action="upload.php" method=post>
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
Upload this file:
<input name="userfile" type="file">
<input type="submit" value="Send File">
</form>
</body>
</html>
//php upload page
<html>
<head>
<title>Uploading...</title>
</head>
<body>
<h1>Uploading file...</h1>
<?
// $userfile is where file went on webserver
// $userfile_name is original file name
// $userfile_size is size in bytes
// $userfile_type is mime type e.g. image/gif text/plain
echo "Variables are:<br>";
echo "userfile: ".$userfile."<br>userfile_name: ".$userfile_name."<br> userfile_size: ".$userfile_size."<br>userfile_type: ".$userfile_type."<br>End variables<br><br>";
if ($userfile=="none")
{
echo "Problem: no file uploaded";
exit;
}
if ($userfile_size==0)
{
echo "Problem: uploaded file is zero length";
exit;
}
if ($userfile_type != "text/plain") //'image/gif' doesnt work correctly
{
echo "Problem: file is not plain text";
exit;
}
$upfile = "images/".$userfile_name;
if ( !copy($userfile, $upfile))
{
echo "Problem: Could not move file into directory";
exit;
}
echo "File uploaded successfully<br><br>";
$fp = fopen($upfile, "r");
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);
$contents = strip_tags($contents);
$fp = fopen($upfile, "w");
fwrite($fp, $contents);
fclose($fp);
echo "Preview of uploaded file contents:<br><hr>";
echo $contents;
echo "<br><hr>";
?>
</body>
</html>
|

August 12th, 2002, 07:02 PM
|
 |
film at 11
|
|
Join Date: Aug 2000
Location: Portland, OR
Posts: 413
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
Maybe I can help without confusing the issue further...
1) The code checks the filetype explicitly, and it has to be "text/plain" or else the script stops. I would do something like this:
PHP Code:
$typesOK = array("text/plain", "image/gif", "image/jpeg", "image/png");
if (! in_array($userfile_type, $typesOK))
{
echo "Problem: file is not of acceptable type.";
exit;
}
2) Your script reads in the data from the uploaded file and dumps it to the screen. You can't display an image just by dumping the data to a web page like that. The script copies the image to a place where it's accessible from the web, I think; all you have to do is put an image tag with the proper URL to display the image.
3) Don't use this check for an empty file upload:
PHP Code:
if ($userfile=="none")
{
echo "Problem: no file uploaded";
exit;
}
Beginning with (I think) version 4.2, when no file is uploaded the variable is no longer set to "none", instead it's just left blank. A better way is to always check the size; if $userfile_size is zero, then no file or an empty file was uploaded.
hth
/nick
|

August 12th, 2002, 08:10 PM
|
|
Contributing User
|
|
Join Date: Aug 2002
Posts: 110
Time spent in forums: 6 h 16 m 17 sec
Reputation Power: 11
|
|
|
That worked out perfectlly, thank you so much...
|

August 12th, 2002, 08:27 PM
|
|
Contributing User
|
|
Join Date: Aug 2002
Posts: 110
Time spent in forums: 6 h 16 m 17 sec
Reputation Power: 11
|
|
|
I have one other question about it though. Do you have any idea why 'image/gif' will upload, but 'image/jpg' and 'image/png' does not work?
$typesOK = array("image/jpeg", "image/png", "image/gif");
if (! in_array($userfile_type, $typesOK))
{
echo "Problem: file is not of acceptable type.";
exit;
}
I am getting this error for jpeg's and png's, but works for gif's. Thanks in advance...
|

August 13th, 2002, 10:15 AM
|
 |
film at 11
|
|
Join Date: Aug 2000
Location: Portland, OR
Posts: 413
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
|
I don't have an answer there.. what's the output of your debugging bit when you upload those types of files? When I wrote a script like this I took all the file types I wanted to accept, uploaded them and copied what PHP said the mime type was...
|

August 13th, 2002, 02:58 PM
|
|
Contributing User
|
|
Join Date: Aug 2002
Posts: 110
Time spent in forums: 6 h 16 m 17 sec
Reputation Power: 11
|
|
|
For some reason the output file types for jpg and png are screwy:
Jpeg output file type prints: 'image/pjpeg'
Png output file type prints: 'image/x-png'
Mabe these are not exceptable MIME types?
|

August 13th, 2002, 03:01 PM
|
 |
film at 11
|
|
Join Date: Aug 2000
Location: Portland, OR
Posts: 413
Time spent in forums: < 1 sec
Reputation Power: 13
|
|
|
Hmm... on closer inspection of my own script I see that I have these listed as acceptable mime types:
image/jpeg
image/pjpeg
image/gif
well, I am not sure why there are seemingly 2 mime types for jpeg. One of those is probably not correct and shouldn't be there. If I find anything else I'll post it here.
|

August 13th, 2002, 03:38 PM
|
|
Contributing User
|
|
Join Date: Aug 2002
Posts: 110
Time spent in forums: 6 h 16 m 17 sec
Reputation Power: 11
|
|
|
sounds good, thanks for all the help.
|

September 5th, 2002, 03:12 PM
|
|
Junior Member
|
|
Join Date: Sep 2002
Location: Boston
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
When investigating validating uploading images, I found this list to certify against and it seems to work. I am not familiar with all the types, but you might find this list helpful.
$cert1 = "image/pjpeg"; //Jpeg type 1
$cert2 = "image/jpeg"; //Jpeg type 2
$cert3 = "image/gif"; //Gif type
$cert4 = "image/ief"; //Ief type
$cert5 = "image/png"; //Png type
$cert6 = "image/tiff"; //Tiff type
$cert7 = "image/bmp"; //Bmp Type
$cert8 = "image/vnd.wap.wbmp"; //Wbmp type
$cert9 = "image/x-cmu-raster"; //Ras type
$cert10 = "image/x-x-portable-anymap"; //Pnm type
$cert11 = "image/x-portable-bitmap"; //Pbm type
$cert12 = "image/x-portable-graymap"; //Pgm type
$cert13 = "image/x-portable-pixmap"; //Ppm type
$cert14 = "image/x-rgb"; //Rgb type
$cert15 = "image/x-xbitmap"; //Xbm type
$cert16 = "image/x-xpixmap"; //Xpm type
$cert17 = "image/x-xwindowdump"; //Xwd type
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|