|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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> |
|
#2
|
||||
|
||||
|
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:
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:
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 |
|
#3
|
|||
|
|||
|
That worked out perfectlly, thank you so much...
|
|
#4
|
|||
|
|||
|
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... |
|
#5
|
||||
|
||||
|
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...
|
|
#6
|
|||
|
|||
|
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? |
|
#7
|
||||
|
||||
|
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. |
|
#8
|
|||
|
|||
|
sounds good, thanks for all the help.
|
|
#9
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > System Administration > FTP Help > ftp upload ware |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|