Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Php File Upload, About uploading files through php
Supa Comix
post Aug 21 2007, 11:19 AM
Post #1


Newbie [ Level 2 ]
Group Icon

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.
Go to the top of the page
 
+Quote Post
kelvinmaki
post Aug 21 2007, 01:17 PM
Post #2


Advanced Member
Group Icon

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. wink.gif

Cheers

Go to the top of the page
 
+Quote Post
Supa Comix
post Aug 21 2007, 02:37 PM
Post #3


Newbie [ Level 2 ]
Group Icon

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! smile.gif
Go to the top of the page
 
+Quote Post
kelvinmaki
post Aug 21 2007, 03:07 PM
Post #4


Advanced Member
Group Icon

Group: Members
Posts: 170
Joined: 30-July 07
Member No.: 23,704



You're welcome. Glad it helped.. wink.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Php Script To Download File From Another Site(9)
  2. Php Reading And Writing To File(4)
  3. How Do I Create And Write To Files?(4)
  4. Online HTML/PHP Editor: Edit File In Browser!(8)
  5. Php : Variables Included Dont Work In Functions(4)
  6. Looking For Upload Scripts That Support Over 150MB(3)
  7. Uploading Image Via Admin Menu?(2)
  8. PHP File Upload Works... But Stupid IE(3)
  9. Display Text If Line Not Empty In Config File(8)
  10. PHP Script To Upload A File(13)
  11. Generating A Table Into A File In CSV Format(6)
  12. Deleting Files With PHP(6)
  13. How To Embed Ram File Produced By Http Header(2)
  14. How To Delete Files When Session Ends(4)
  15. Xgrid With Php(0)
  1. Updating An Rss File Using A Php Form(1)
  2. How To Edit Php Files?(16)
  3. File Self Secure?(6)
  4. How To Delete File Using PHP Shell Script(3)
  5. How To Force A Zip File To Be Downloaded(11)
  6. Automated File Structure Creation Script(3)
  7. Help To Transfer Files Within Hosting Space Using Php(4)
  8. Php Uploading Problem(6)
  9. Extplorer(7)
  10. Reading Files And Folders(1)
  11. How To Protect Included Files(10)
  12. Magic Quotes And $_files(3)


 



- Lo-Fi Version Time is now: 21st August 2008 - 10:43 PM