Page 1 of 1
Uploads and Downloads files
Posted: Wed Mar 24, 2004 2:33 am
by RedSouljaz
I have just made the uploads file which are allows the user to uploads any file such as textfile, images, powerpoint or zip file.
The uploads file is working properly, the problem is after I upload it and I try to open for example image in the upload folder I can open it and itcomes out an error message.besides that he file size is decreasing also. So is it because the file had been encrypted?
How to download file that I had been upload to the original file size?
Does anyone have any solution or idea, thanks
Posted: Wed Mar 24, 2004 2:34 am
by Goowe
What is the error message you get when you view the file?
Posted: Wed Mar 24, 2004 6:36 am
by RedSouljaz
The error message when I try to view it or download it is file is currupted. I dont know why, is it maybe I used the wrong method to download it, I'm using this code:
<?php
$current_dir = 'uploads/';
$dir = opendir($current_dir);
echo "Upload directory is $current_dir<br />";
echo 'Directory Listing:<br /><hr /><br />';
while ($file = readdir($dir))
{
echo "<a href = uploads/$file>$file</a><br />";
}
echo '<h1 /><br />';
closedir($dir);
?>
Posted: Wed Mar 24, 2004 6:57 am
by Fjook
It does not seem that the download is wrong, I would say it is probably the upload by the nature of your error. Firstly, if you are using a form make sure it has the enctype attribute, like so:
Code: Select all
<form enctype="multipart/form-data" action="_URL_" method="post">
Also, I recommend that you take a look at this page of the PHP manual (as I can't answer this directly - I don't know it enough) and especially read the user notes. It talks about different things, including the way browsers interpret file types differently (in the header, example: image/png and image/x-png), and things like uploading ascii/binary format, which I think may be the key to your problem. Either way, worth a look:
http://ca3.php.net/features.file-upload
Yours,
Fjook!
Posted: Wed Mar 24, 2004 8:10 am
by RedSouljaz
I did use everything in that website, this is the code:
upload.html:
<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">
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Upload File 1:
<input name="userfile[]" type="file">
</p>
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">
Upload File 2:
<input name="userfile[]" type="file">
</p>
<p>
<input type="submit" value="Send File">
</p>
</form>
</body>
</html>
upload.php
<?php // $userfile is where file went on webserver
for($i = 0; $i < 2; $i++)
{
$userfile[$i] = $HTTP_POST_FILES['userfile']['tmp_name'][$i]; // $userfile_name is original file name
$userfile_name[$i] = $HTTP_POST_FILES['userfile']['name'][$i];
// $userfile_size is size in bytes
$userfile_size[$i] = $HTTP_POST_FILES['userfile']['size'][$i];
// $userfile_type is mime type e.g. image/gif
$userfile_type[$i] = $HTTP_POST_FILES['userfile']['type'][$i];
// $userfile_error is any error encountered
$userfile_error[$i] = $HTTP_POST_FILES['userfile']['error'][$i];
// userfile_error was introduced at PHP 4.2.0
// use this code with newer versions
if ($userfile_error[$i] > 0)
{
echo 'Problem: ';
switch ($userfile_error[$i])
{
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;
}
// end of code for 4.2.0
// prior to 4.2.0 use manual error checking as below
/*
if ($userfile=='none')
{
echo 'Problem: no file uploaded';
exit;
}
if ($userfile_size==0)
{
echo 'Problem: uploaded file is zero length';
exit;
}
*/
// end older version error checking
// put the file where we'd like it
$upfile[$i] = 'uploads/'.$userfile_name[$i];
// is_uploaded_file and move_uploaded_file added at version 4.0.3
if (is_uploaded_file($userfile[$i]))
{
if (!move_uploaded_file($userfile[$i], $upfile[$i]))
{
echo 'Problem: Could not move file to destination directory';
exit;
}
}
else
{
echo 'Problem: Possible file upload attack. Filename: '.$userfile_name[$i];
exit;
}
// older versions code as recommended in PHP manual
/*
function is_uploaded_file($filename) {
if (!$tmp_file = get_cfg_var('upload_tmp_dir')) {
$tmp_file = dirname(tempnam('', ''));
}
$tmp_file .= '/' . basename($filename);
// User might have trailing slash in php.ini...
return (ereg_replace('/+', '/', $tmp_file) == $filename);
}
if (is_uploaded_file($userfile))
{
copy($userfile, $upfile);
} else
{
echo 'Problem: Possible file upload attack. Filename: '.$userfile_name';
}
*/
// end older version
echo 'File uploaded successfully<br /><br />';
// reformat the file contents
$fp[$i] = fopen($upfile[$i], 'r');
$contents[$i] = fread ($fp[$i], filesize ($upfile[$i]));
fclose ($fp[$i]);
$contents[$i] = strip_tags($contents[$i]);
$fp[$i] = fopen($upfile[$i], 'w');
fwrite($fp[$i], $contents[$i]);
fclose($fp[$i]);
// show what was uploaded
echo $userfile_type[$i];
echo 'Preview of uploaded file contents:<br /><hr />';
echo $contents[$i];
echo '<br /><hr />';
}
?>
</body>
And it is working, the only problem is when i open the file only the one that has an extension .txt can be open, the rest will be corrupter.
Is the code for uploading the image and text is different?
Do I suppose to get the actual size of the file after I upload the file or not?
Posted: Wed Mar 24, 2004 12:47 pm
by Fjook
Yes I think so,
I think its because it uploads it in ASCII format by default and you want it in Binary instead. I'm not sure how to do that, but let me look around, if I happen to find it before you, I'll post!
BTW, I don't think the file size should matter.