Jump to content

[SOLVED] Multiple File Uploads


eRott

Recommended Posts

Hey,

 

Okay so I am trying to upload two images from a form. Each one has a different name and each one has to go to a different location. If I use the exact same image for both fields, everything works fine. However, if I use two different images for both fields (which is what is supposed to happen) it doesn't work. Any ideas?

 

EDIT: The problem occurs with imgepisode. The thumbnail imgthumb uploads correctly. I cannot use an array because each file has to go to a different location and must have a different name. The name / location of the uploaded file is then saved to a database.

 

echo "
<form method='post' enctype='multipart/form-data' name='newepisode'>
<b>Title:</b><br /><input name='title' type='text' id='title' size='15' /><br /><br />
<b>Synopsis:</b><br /><textarea name='synopsis' id='synopsis' cols='60' rows='10' /></textarea><br /><br />
<b>Episode:</b><br /><input name='imgepisode' type='file' /><br /><br />
<b>Thumbnail:</b><br /><input name='imgthumb' type='file' /><br /><br />
<input type='submit' name='add' id='add' value='Add Episode' /><br /><br />
</form>
";

 

if(isset($_POST['add'])) {

	srand ((double) microtime( )*1000000);
	$rnum = rand( );
	$date = time();
	$char = "_";

	if (@is_uploaded_file($_FILES["imgepisode"]["tmp_name"])) {
		copy($_FILES["imgepisode"]["tmp_name"], "../episodes/" . $rnum . $char . $date . $char . $_FILES["imgepisode"]["name"]);
	} else {
		echo "ERROR: Episode image could not be uploaded.";
		include("../global/admin-footer.php");
		die;
	}

	if (@is_uploaded_file($_FILES["imgthumb"]["tmp_name"])) {
		copy($_FILES["imgthumb"]["tmp_name"], "../episodes/thumbs/" . $rnum . $char . $date . $char . $_FILES["imgthumb"]["name"]);
	} else {
		echo "ERROR: Thumbnail image could not be uploaded.";
		include("../global/admin-footer.php");
		die;
	}

	$src_episode = $rnum . $char . $date . $char . $_FILES["imgepisode"]["name"];
	$src_thumb = $rnum . $char . $date . $char . $_FILES["imgthumb"]["name"];
	$title = $_POST['title'];
	$synopsis = $_POST['synopsis'];
	$author = $_SESSION['user'];

	if (empty($title) || empty($synopsis) || empty($author)) {
		echo "<b>Error:</b> Please ensure all fields are filled out correctly and try re-submitting the episode.";
	} else {
		$query = "INSERT INTO episodes (title, synopsis, date, author, src_episode, src_thumb) VALUES ('$title', '$synopsis', '$date', '$author', '$src_episode', '$src_thumb')";
		mysql_query($query) or die(mysql_error());
		echo "<meta http-equiv='refresh' content='1;url=function-episode.php?task=New'>Episode successfully added!";
	}

}

 

Thanks.

Take care.

Link to comment
Share on other sites

Just the one I defined.

 

echo "ERROR: Episode image could not be uploaded.";

 

EDIT: Not really. I dont think I could use move_uploaded_file() as that is not what I am doing by using is_uploaded_file(). I am checking to see if the file has been uploaded (via HTTP POST) and then if it has been, I go ahead and use copy() to well, copy the file to the server.

 

EDIT: Oh. I see what you mean.

Link to comment
Share on other sites

I hate that you cannot modify a post after what is it, 60 seconds have passed. Either way, its far too short.

 

Anyway, using move_uploaded_file, considering it does essentially the same thing, has no effect. While the thumbnail image is uploaded, the episode image is not and I am not too sure why.

 

Is there some standard error reporting for copy or is_uploaded_file? Otherwise, as I've mentioned. The only error it provides is the one I told it to provide if there was an error.

 

ERROR: Episode image could not be uploaded.

 

Any ideas?

Link to comment
Share on other sites

Your code has no error checking if the upload worked or not before attempting to access the uploaded file. You need to check the ['error'] element and only proceed when there are no errors - http://www.php.net/manual/en/features.file-upload.errors.php

 

Also, turn on full php error reporting to get php to display any errors produced when the code is running. Add these two lines after your first opening <?php tag -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

Link to comment
Share on other sites

Okay, so, I have taken a look at that page and while I've attempted to add the error checking, it doesnt actually seem to work. No errors are being returned.

 

	$uploadErrors = array(
		UPLOAD_ERR_OK => 'There is no error, the file uploaded with success.',
		UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
		UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
		UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
		UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
		UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
		UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
		UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.',
	);

	srand ((double) microtime( )*1000000);
	$rnum = rand( );
	$date = time();
	$char = "_";

	if (is_uploaded_file($_FILES["img_episode"]["tmp_name"])) {
		$errorCode = $_FILES['img_episode']['error'];
		if($errorCode !== UPLOAD_ERR_OK && isset($uploadErrors[$errorCode])) {
			throw new Exception($uploadErrors[$errorCode]);
			include("../global/admin-footer.php");
			die;
		} else {
			copy($_FILES["img_episode"]["tmp_name"], "../episodes/" . $rnum . $char . $date . $char . $_FILES["img_episode"]["name"]);
		}
	}

	if (is_uploaded_file($_FILES["img_thumb"]["tmp_name"])) {
		$errorCode = $_FILES['img_thumb']['error'];
		if($errorCode !== UPLOAD_ERR_OK && isset($uploadErrors[$errorCode])) {
			throw new Exception($uploadErrors[$errorCode]);
			include("../global/admin-footer.php");
			die;
		} else {
			copy($_FILES["img_thumb"]["tmp_name"], "../episodes/thumbs/" . $rnum . $char . $date . $char . $_FILES["img_thumb"]["name"]);
		}
	}

 

Also, I am not sure how I got it to appear, however it would seem the problem is with the php settings. The size of the episode image is larger then the value of upload_max_filesize for the server. To test this, I used two small images and it worked. The default value on my server is "2M". The size of the episode image is 2.28 MB.

 

So two questions, what am I doing wrong with the error checking and is the upload_max_filesize value configurable?

 

ini_set ("upload_max_filesize", "5M");

 

The above does no good.

 

Thanks.

Take care.

Link to comment
Share on other sites

error checking if the upload worked or not before attempting to access the uploaded file.
You put the error checking inside of a conditional test that would only be true if the upload worked.

 

Error checking first.  ::)

 

upload_max_filesize is not settable in a script using ini_set -

 

upload_max_filesize changeable:PHP_INI_PERDIR

 

PHP_INI_PERDIR: Entry can be set in php.ini, .htaccess or httpd.conf
Link to comment
Share on other sites

EDIT: Okay, I failed to realize that upload_max_filesize is PHP_INI_PERDIR so it can only be set / configured in a php.ini, .htaccess or httpd.conf file.

 

So I added a php.ini file in the same directory as the upload page and added:

 

upload_max_filesize = 5M

 

Mind you it still doesn't seem to work. So the question remains, is that the proper way to set the upload_max_filesize in php.ini and how do I get the form error checking to work?

 

EDIT2: Right. lol. I had the error checking before the uploads but it looked odd to me. Thats when I saw the error message about UPLOAD_ERR_INI_SIZE showed up.

 

Thanks.

Take care.

Link to comment
Share on other sites

Ok, but before anything else, I have no idea what I am doing with this error validating.

 

Here's the code:

 

	$uploadErrors = array(
		UPLOAD_ERR_OK => 'There is no error, the file uploaded with success.',
		UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
		UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
		UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
		UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
		UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
		UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
		UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.',
	);

	srand ((double) microtime( )*1000000);
	$rnum = rand( );
	$date = time();
	$char = "_";

	$ep_errorCode = $_FILES['img_episode']['error'];
	$tb_errorCode = $_FILES['img_thumb']['error'];

	if($ep_errorCode !== UPLOAD_ERR_OK && isset($uploadErrors[$ep_errorCode])) {
		throw new Exception($uploadErrors[$ep_errorCode]);
		include("../global/admin-footer.php");
		die;
	} else {
		if (is_uploaded_file($_FILES["img_episode"]["tmp_name"])) {
			copy($_FILES["img_episode"]["tmp_name"], "../episodes/" . $rnum . $char . $date . $char . $_FILES["img_episode"]["name"]);
		} else {
			echo "ERROR: Episode image could not be uploaded.";
			include("../global/admin-footer.php");
			die;
		}
	}

	if($tb_errorCode !== UPLOAD_ERR_OK && isset($uploadErrors[$tb_errorCode])) {
		throw new Exception($uploadErrors[$tb_errorCode]);
		include("../global/admin-footer.php");
		die;
	} else {
		if (is_uploaded_file($_FILES["img_thumb"]["tmp_name"])) {
			copy($_FILES["img_thumb"]["tmp_name"], "../episodes/thumbs/" . $rnum . $char . $date . $char . $_FILES["img_thumb"]["name"]);
		} else {
			echo "ERROR: Thumbnail image could not be uploaded.";
			include("../global/admin-footer.php");
			die;
		}
	}

 

Here's the result. Hence why I had placed it inside the conditional statement as I have no idea what to do with it.

 

Fatal error: Uncaught exception 'Exception' with message 'The uploaded file exceeds the upload_max_filesize directive in php.ini.' in /home/REMOVED/public_html/admin/function-episode.php:72 Stack trace: #0 {main} thrown in /home/REMOVED/public_html/admin/function-episode.php on line 72

 

Line 72 is:

 

throw new Exception($uploadErrors[$ep_errorCode]);

 

And here is php.ini which is in public_html/

 

upload_max_filesize = 5M

 

What am I doing wrong?

 

Thanks.

Link to comment
Share on other sites

Okay, so I fixed the problem. The reason editing the value for upload_max_filesize wouldn't work is because my host is not running phpSuExec and therefore I cannot use php.ini files to control PHP settings. I had to use a .htaccess file.

 

But on another note, while no longer necessary, I would still like to know what I was doing wrong with the form validating if anyone has an answer.

 

Thanks for all the help.

Take care.

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.