bookmark - Php File Upload About uploading files through php

Php File Upload - About uploading files through php

 
 Discussion by Supa Comix with 3 Replies.
 Last Update: August 21, 2007, 3:07 pm
 
bookmark - Php File Upload About uploading files through php  
    
free web hosting
 
Right i have done a check for a tutorial on this as well as a question about it but php is not allowed in the search box. So i thought i'd just ask what i want to know.

I have a form which uploads a file, it refreshes the page, uploads the file and then alerts the user to if the file has uploaded. To be honest im not sure why i keep getting the error. But here is the code:

This is the form that is used for the user to select the file

CODE

<form enctype="multipart/form-data" action="fileupload.php?op=up&user=<?php echo($user); ?>&fid=<?php echo($fid); ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
Choose a file to upload:
<input name="uploaded" type="file" id="uploaded" />
<input type="submit" value="Upload File" />
</form>


This is the upload code

CODE

if ($op == "up"){
$target = "uploads/";
$target = $target . basename($_FILES['uploaded']['name']);
echo($target);
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo("The file ") . basename ($_FILES['uploadedfile']['name']). "has been uploaded!";
$filesize = $uploaded_size;
$sql = mysql_query("UPDATE tbl_files SET filesize='$filesize' WHERE fileid='$fid'");
}
else {
echo ("Sorry, there was a problem uploading your file.");
}
}


If you could point out any problems with it please do.

Tue Aug 21, 2007    Reply    New Discussion   


Firstly, maybe you could point out what is the error you are getting. From these codes below,

CODE

<form enctype="multipart/form-data" action="fileupload.php?op=up&user=<?php echo($user); ?>&fid=<?php echo($fid); ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
Choose a file to upload:
<input name="uploaded" type="file" id="uploaded" />
<input type="submit" value="Upload File" />
</form>


It seems to be alright. Of course I assume that the variable $user and $fid is declared somewhere.

Secondly, as for the codes that really does the uploading,

CODE

if ($op == "up"){
$target = "uploads/";
$target = $target . basename($_FILES['uploaded']['name']);
echo($target);
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo("The file ") . basename ($_FILES['uploadedfile']['name']). "has been uploaded!";
$filesize = $uploaded_size;
$sql = mysql_query("UPDATE tbl_files SET filesize='$filesize' WHERE fileid='$fid'");
}
else {
echo ("Sorry, there was a problem uploading your file.");
}
}


If you are going to upload to folder uploads/, then you need to allow permission to write to that folder. Do a chmod for that folder. Did an upload once and found out that its due to permission, it prevent any files from writing to that directory. And of course before all this, you need to mkdir that directory.

There's once I encounter blank page after uploading the files. It didn't show any errors and just blank. This can be caused by trying to upload a huge filesize. If you are running this on localhost, then it should be fine. But if you want to upload a huge file to the server (eg. web hosting), you will need to set the memory limit in the php setup configuration to a higher value. That you can contact the administrator of the web hosting. However, there's another alternative to this approach. You can set the .htaccess file as well

CODE

<FilesMatch "\.(php|html?)$">
php_value memory_limit 32M
</FilesMatch>


Put the above line to your .htaccess at the root. This method solve for me.

By the way, these are all the problem I encounter for uploading a file. If you encounter the same problem, this might help abit. If not, tell us your error in details. What did you do that got the error and what is the error message. :blink:

Cheers

Tue Aug 21, 2007    Reply    New Discussion   

Awesome. Thanks dude i tried that stuff and it now works! Thanks a lot! :blink:

Tue Aug 21, 2007    Reply    New Discussion   

You're welcome. Glad it helped.. :blink:

Tue Aug 21, 2007    Reply    New Discussion   


Quickly Post to Php File Upload About uploading files through php w/o signup Share Info about Php File Upload About uploading files through php using Facebook, Twitter etc. email your friend about Php File Upload About uploading files through php Print
Reply / Comment Ask a Question? Share / Bookmark E-Mail a Friend Print

Bbcode Help Making BBCode  Bbcode Help Making BBCode (9) (2) How To Disable Safe Mode In Direct Admin.   How To Disable Safe Mode In Direct Admin.