Jump to content
xisto Community
Sign in to follow this  
Supa Comix

Php File Upload About uploading files through php

Recommended Posts

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

<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

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.

Share this post


Link to post
Share on other sites

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

<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,

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

<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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.