uploading pictures with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
verycleanteeth
Forum Newbie
Posts: 8
Joined: Thu Mar 11, 2004 1:15 am

uploading pictures with PHP

Post by verycleanteeth »

Alright. I'll state up front, I'm a damn n00b.
Anyways, I created a link on my webpage to upload files, but when anyone uploads a picture, all I get are these tiny (4-10 bytes) nothing files. I'm not sure why the pictures are being 'truncated' (if that's an appropriate term here). Here is my code for 'upload.php':

<?php
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
$userfile_name = $HTTP_POST_FILES['userfile']['name'];
$userfile_size = $HTTP_POST_FILES['userfile']['size'];
$userfile_type = $HTTP_POST_FILES['userfile']['type'];
$userfile_error = $HTTP_POST_FILES['userfile']['error'];

if ($userfile_error > 0)
{
echo 'Problem, Yo: ';
switch ($userfile_error)
{
case 1: echo 'File exceeded upload_max_filesize'; break;
case 2: echo 'File exceeded max_file_size'; break;
case 3: echo 'File only partially uploaded'; break;
case 4: echo 'No file uploaded'; break;
}
exit;
}
$upfile = 'uploaded/'.$userfile_name;

if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: '.$userfile_name;
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 />';

?>





and here is the snippet of code from the html page that links to upload.php.



<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />


I would like to make it so that when a picture is uploaded, it is displayed on upload.php, but I guess I have to make it upload correctly first, huh?
Thanks in advance to anybody who decides to help this damned n00b.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$contents = strip_tags($contents); ? Why are you stripping html tags from a binary image file? That would cause corruption. Also you can't just echo binary image data ( echo $contents ), but none of that should affect the uploaded file size ... does the file size look ok in a var_dump($_FILES); after the upload has completed?
bodge
Forum Newbie
Posts: 7
Joined: Thu Mar 11, 2004 6:13 pm

why work so hard

Post by bodge »

Why are you working so hard to upload a picture?

just enter:

copy("$userfile", "./uploaded/$userfile_name")
or die("Error");


you can also set the max size on the html form so you dont have to wait for the file to be uploaded to check its size.
verycleanteeth
Forum Newbie
Posts: 8
Joined: Thu Mar 11, 2004 1:15 am

Post by verycleanteeth »

the anwer to both questions is: because I don't know what the hell i'm doing.
I removed $contents = strip_tags($contents); and that was what was corrupting the files.

thanks for the help :)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

verycleanteeth wrote:the anwer to both questions is: because I don't know what the hell i'm doing.
if its any consolation to you, that made me laugh. :wink:
Post Reply