Page 1 of 1

uploading pictures with PHP

Posted: Thu Mar 11, 2004 1:15 am
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.

Posted: Thu Mar 11, 2004 1:21 am
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?

why work so hard

Posted: Thu Mar 11, 2004 6:13 pm
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.

Posted: Thu Mar 11, 2004 6:43 pm
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 :)

Posted: Thu Mar 11, 2004 6:46 pm
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: