Jump to content

File form not uploading correctly


BillyBoB

Recommended Posts

I am working on building a site that needs to upload a pdf to their site weekly. The file is not uploading correctly, could you help me out and see if there are any errors. I have already set /resources/ and /resources/familyties/ to 777...

 

Error Messages:

Warning:  move_uploaded_file(/resources/familyties/01_08_2010.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/cplpaso0/berrysch/public_html/Members on line 111

Warning:  move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpJe0INn' to '/resources/familyties/01_08_2010.pdf' in /home/cplpaso0/berrysch/public_html/Members on line 111

 

Code:

$uploadfile = '/resources/familyties/'.substr($_POST['date'], 0, 2).'_'.substr($_POST['date'], 3, 2).'_'.substr($_POST['date'], 6, 4).'.pdf';
$_POST['date']=substr($_POST['date'], 6, 4).'-'.substr($_POST['date'], 3, 2).'-'.substr($_POST['date'], 0, 2);
if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
if($sql = mysql_query("INSERT INTO `dyn_familyties` (`date`, `link`) VALUES('$_POST[date]', '')")) {
	echo("
		<div>
			$_POST[date] was added to the family ties list. <a href=\"/~berrysch/Members/FamilyTies\">Go back</a>
		</div>
	");
}else{
	echo("
		<form method=\"post\" enctype=\"multipart/form-data\">
			<div style=\"text-align: center;\">There was an error adding $_POST[date] to the database!</div>
			<div>
				<div style=\"float: left; width: 102px;\"><input type=\"text\" name=\"date\" class=\"inputOpt\" style=\"width: 88px;\" value=\"MM/DD/YYYY\" /></div>
				<div style=\"float: left;\"><input type=\"file\" name=\"file\" class=\"inputOpt\"/></div>
				<div style=\"float: right;\"><input type=\"submit\" name=\"ft_submit\" class=\"inputSub\" value=\"Submit\" /></div>
				<div style=\"clear: both; font: normal 0px Arial;\"> </div>
			</div>
		</form>
	");
}
}else{
print_r($_FILES);
echo("
	<form method=\"post\" enctype=\"multipart/form-data\">
		<div style=\"text-align: center;\">There was an error uploading ".$_FILES['file']['name']." to the server!</div>
		<div>
			<div style=\"float: left; width: 102px;\"><input type=\"text\" name=\"date\" class=\"inputOpt\" style=\"width: 88px;\" value=\"MM/DD/YYYY\" /></div>
			<div style=\"float: left;\"><input type=\"file\" name=\"file\" class=\"inputOpt\"/></div>
			<div style=\"float: right;\"><input type=\"submit\" name=\"ft_submit\" class=\"inputSub\" value=\"Submit\" /></div>
			<div style=\"clear: both; font: normal 0px Arial;\"> </div>
		</div>
	</form>
");
}

Link to comment
Share on other sites

Ok the error is your naming scheme of the file. What your code is doing is it is trying to say that a file is there when it is really not.

 

This line here is your problem:

 

$uploadfile = '/resources/familyties/'.substr($_POST['date'], 0, 2).'_'.substr($_POST['date'], 3, 2).'_'.substr($_POST['date'], 6, 4).'.pdf' ;

 

The reason this is wrong is because the first line of code is naming a file by the current date which is then making a file that does not exists on the server therefore you are getting those dreadful error messages. Try changing your code to this:

 

$uploadfile = "/resources/familyties/{$_FILES['file']['name']}" ;

 

What this does is whatever the name is of the file the person is uploading is what the name will be on the server. Also, you were trying to name the file being uploaded with the extension of a ".pdf" but if the file the person is uploading is not a true ".pdf" it will not matter if you append the ".pdf" extension to the file being uploaded. If you want to only allow pdf files to be uploaded you should specify the mime type that is allowed with the file upload. For example:

 

if ($_FILES['file']['type'] != "application/pdf") {
echo "<p>Your file could not be uploaded because: You can only upload PDF's.</p>" ;
}

 

Also, if you want to rename the file being uploaded, you might want to look at the rename function: http://php.net/manual/en/function.rename.php

Link to comment
Share on other sites

The fix didn't work. It still throws the same error.

 

Warning:  move_uploaded_file(/resources/familyties/0604_betty_paige_ebay.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/cplpaso0/berrysch/public_html/Members on line 111

Warning:  move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php2WoBpR' to '/resources/familyties/0604_betty_paige_ebay.jpg' in /home/cplpaso0/berrysch/public_html/Members on line 111

Link to comment
Share on other sites

The fix didn't work. It still throws the same error.

 

Warning:  move_uploaded_file(/resources/familyties/0604_betty_paige_ebay.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/cplpaso0/berrysch/public_html/Members on line 111

Warning:  move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php2WoBpR' to '/resources/familyties/0604_betty_paige_ebay.jpg' in /home/cplpaso0/berrysch/public_html/Members on line 111

 

Here check out these 2 links. These should solve your problem:

 

http://www.php.net/manual/en/function.move-uploaded-file.php#77661

http://www.php.net/manual/en/function.move-uploaded-file.php#95084

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.