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.
uploading pictures with PHP
Moderator: General Moderators
-
verycleanteeth
- Forum Newbie
- Posts: 8
- Joined: Thu Mar 11, 2004 1:15 am
$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
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.
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