• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

file upload

I am trying to set up a form to upload pdf files to my site. It will be in a password protected folder so I don't need security, but it keeps telling me the file has been uploaded, without the file name, and the file has not actually been uploaded.
upload_files.php
<?php 
$target = "docs/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} 
else {
echo "Sorry, there was a problem uploading your file.";
}
?> 
 
-------------------------------------------------------------
My form
 
<form enctype="multipart/form-data" action="upload_files.php" method="POST">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>Single File Upload </strong></td>
</tr>
<tr>
<td>Select file 
<input name="uploaded" type="file" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>

Open in new window

0
sendhelp
Asked:
sendhelp
  • 5
  • 2
1 Solution
 
Ray PaseurCommented:
I'll post my standard upload script in a moment.  It covers the waterfront!
0
 
Ray PaseurCommented:
Please let me know if you have any questions.  Best regards, ~Ray
<?php // RAY_upload_demo.php
 
// NAME OF THE DIRECTORY FOR UPLOADS
$uploads = 'uploads';
 
// UPLOAD ERRORS
$errors	= array(
	0=>"<span>Success!</span>",
	1=>"<span class=\"required\">The uploaded file exceeds the upload_max_filesize directive in php.ini</span>",
	2=>"<span class=\"required\">The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form</span>",
	3=>"<span class=\"required\">The uploaded file was only partially uploaded</span>",
	4=>"<span class=\"required\">No file was uploaded</span>",
	6=>"<span class=\"required\">Missing a temporary folder</span>",
	7=>"<span class=\"required\">Cannot write file to disk</span>"
);
 
// IF NO UPLOAD HAS HAPPENED, PUT UP THE FORM
if ( (empty($_GET['p'])) && (empty($_POST['MAX_FILE_SIZE'])) ) { ?>
 
	<h2>Welcome to File Upload Demo</h2>
	<!-- nb:enctype -->
	<form name="UploadForm" enctype="multipart/form-data" action="<?=$_SERVER["REQUEST_URI"]?>?p=1" method="POST">
	<!-- MAX_FILE_SIZE must precede the file input field -->
	<input type="hidden" name="MAX_FILE_SIZE" value="8192000" />
	<!-- Name of input element is userfile - determines name in $_FILES array -->
 
	Find the file and click the "Upload" button below.<br />
	File Name:&nbsp; <input name="userfile" type="file" size="80" /> &nbsp; <input type="submit" name="_submit" value="Upload" />
	</form>
<?php
 
	die();
 
} else {
// WE HAVE AN UPLOADED FILE - PUT IT SOMEWHERE SAFE
 
// DATE TIME STAMP BECOMES PART OF FILE NAME
	$filedate	= date('Ymd\THis');
 
// SYNTHESIZE A NEW FILE NAME (MAYBE USE THE CLIENT USER ID, IF ONE EXISTS)
	$f_type	= trim(strtolower(end(explode( '.', basename($_FILES['userfile']['name'] )))));
	$my_new_file	= getcwd() . '/' . $uploads . '/' . $filedate . '.'. $f_type;
	$my_file	=                  $uploads . '/' . $filedate . '.'. $f_type;
 
// MOVE THE FILE INTO THE DIRECTORY
	$error_code	= $_FILES["userfile"]["error"];
	$file_size	= number_format($_FILES["userfile"]["size"]);
 
// PAGE HEADER
	echo "<h2>File Upload Demo</h2>\n";
 
// CHECK UPLOAD ERROR CODE
	if ($error_code != 0) {
		$error_message = $errors[$error_code];
		echo "<p>Upload Error Code: $error_code: $error_message</p>\n";
		echo "<p><a href=\"$_SERVER[PHP_SELF]\">Try again?</a></p>\n";
		die();
	}
 
// REPORT STATUS OF UPLOAD
	if (move_uploaded_file($_FILES['userfile']['tmp_name'], $my_new_file)) {
		echo "<p>\n";
		echo "UPLOAD COMPLETE: \n";
		echo "$file_size bytes saved in $my_file.\n";
		echo "</p>\n";
		chmod ($my_new_file, 0755);
	} else {
		echo "<p>\n";
		echo "Merde! function move_uploaded_file() to $my_new_file failed\n";
		echo "</p>\n";
		die();
	}
}
 
// PROCESS THE INFORMATION IN THE UPLOADED FILE...
 
?>

Open in new window

0
 
sendhelpAuthor Commented:
You are awesome!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ray PaseurCommented:
sendhelp: Thanks for the points!  Best regards, ~Ray
0
 
whspiderCommented:
the code works perfectly fine for small files
But when I upload files of size around 25Mb
I get the following  error

Merde! function move_uploaded_file() to /home/travel/public_html/upload/upload/20091023T092253. failed
0
 
Ray PaseurCommented:
Check the file size in the script and also run phpinfo() and check for max post size, max file size, etc. in there.  Your uploads are getting throttled by the server or by the PHP settings.  Could be a lot of things wrong with file uploads of that size!
0
 
whspiderCommented:
I increased the max upload size.

A file of this size cannot be uploaded directly from forms ?
0
 
Ray PaseurCommented:
You should consider opening a question about this!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now