|
|
|
|
![]() ![]() |
Aug 21 2007, 11:19 AM
Post
#1
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 24 Joined: 9-August 07 Member No.: 23,954 |
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. |
|
|
|
Aug 21 2007, 01:17 PM
Post
#2
|
|
|
Advanced Member Group: Members Posts: 170 Joined: 30-July 07 Member No.: 23,704 |
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. Cheers |
|
|
|
Aug 21 2007, 02:37 PM
Post
#3
|
|
|
Newbie [ Level 2 ] Group: Members Posts: 24 Joined: 9-August 07 Member No.: 23,954 |
Awesome. Thanks dude i tried that stuff and it now works! Thanks a lot!
|
|
|
|
Aug 21 2007, 03:07 PM
Post
#4
|
|
|
Advanced Member Group: Members Posts: 170 Joined: 30-July 07 Member No.: 23,704 |
You're welcome. Glad it helped..
|
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 21st August 2008 - 10:43 PM |