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