Struggling to Locate File Upload Error

I think the error is possibly the size of the upload. It’s 15MB.

I’m running Windows Plesk, not a great deal of support available.

When I upload a small PDF file, say 1MB it uploads correctly, but uploading larger PDF files just gives me a 500 Internal Server Error. There’s nothing in the log that indicates what the problem is either.

Here’s my code:

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



	if ($_FILES['pdf']['type'] != "application/pdf") {

// Type is not PDF display error

	echo '<h2><strong>Error</strong> We can only accept PDF files at this time.</h2>';

} else {




	// use timestamp to create unique title

	$random_digit=time();

	// declare where to upload the file

	 $targeta="../publication/" . $userid . "/$_GET[id]/".$random_digit."_".$_FILES['pdf']['name'];

	// move the file

	if(move_uploaded_file($_FILES['pdf']['tmp_name'],$targeta)){

	// use timestamp to create unique title

	$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 60000)
&& in_array($extension, $allowedExts))
  {

// Type is not PDF display error

	echo '<h2><strong>Error</strong> We can only accept PDF files at this time.</h2>';

} else {

	// declare where to upload the file

	 $target="../publication/" . $userid . "/$_GET[id]/".$random_digit."_".$_FILES['img']['name'];

	// move the file
	if(move_uploaded_file($_FILES['img']['tmp_name'],$target)){



	// file has been uploaded, set message
		$dialog = '<h2><strong>Success</strong> Your publication has been uploaded and is now awaiting moderation.</h2>';

	// get string version of UK date
	$date = date('m-d-Y');

	// insert info to the db
	$sth = $dbconn->prepare("
	INSERT INTO publication_issue
	(user_id, publication_id, issue_number, img, smallimg, pdf, upload_date, cat, viewcount, status, mod_status)
	VALUES     (:user_id,:publication_id,:issue_number,:img,:smallimg,:pdf,:upload_date,:cat,:viewcount,:status,:mod_status)
	
	");


	
	$params = array(
	user_id => $userid,
	publication_id => $_GET[id],
	issue_number => $_POST[issue_number],
	img => $target,
	smallimg => 0,
	pdf => $targeta,
	upload_date => $date,
	cat => $_POST[category],
	viewcount => 0,
	status => 1,
	mod_status => 2
	);
	$sth->execute($params);
	
        $result = $sth->fetch();
       //echo $dbconn->lastInsertId();
       echo $newid;
       $ex = true;

 }
else{
	//There's an error
		echo '<h2><strong>Error</strong> There was a problem uploading your publication.</h2>';

	}
}
	if ($ex = true){
	  //Everything inserted correctly, add image and update db

	}}}
}

It’s not hitting any of the errors I have in place leading me to believe there’s nothing wrong with the code and I can’t think of any other reasons other than the size of the publication or the execution time.

I have tried to change this in the PHP INI file but to no avail.

PHP.INI:


max_execution_time	600
memory_limit	128M
upload_max_filesize	52428800

Are there any other things I can do to try and resolve this issue?

Thanks

Before processing (moving) the uploaded file, check to see if an error occurred. Use:

$_FILES['pdf']['error']

Error Messages Explained

Thanks but no output.

There’s one more ini setting that has to be set to the size you want to allow: post_max_size

Thanks, changed it to 50M but still throws out 500 Internal Server Error. Uploading smaller files work fine.

Just an update, it appears I wasn’t logging errors so I’ve changed the value in PHPINI and now have:

error_log D:\Plesk\Additional\PleskPHP5\phperror.txt
error_reporting 2048
log_errors On
log_errors_max_len 1024
file_uploads On
upload_max_filesize 50M
upload_tmp_dir no value
post_max_size 50M

Not sure why there’s no tmp dir, but I can upload smaller PDF files so don’t think it’s this. Having added the error log, I tried to upload again and got the expected 500 error so I navigated to the log and it’s empty. Not sure what else I can do?

Like Sherlock Holmes, track down the bug. :lol:

Have you added the error checking as I suggested above? Have your script print out the error. The error 500 will prevent it from being output so kill the script right after:

$error = $_FILES['pdf']['error'];
echo "File error code= ".$error;
die();

Another way to discover bug evidence is to print out the $_FILES variable:

print_r $_FILES['pdf'];
die();

This code should be the first thing in the script. If the error 500 persists you know it’s the upload and not your code. If the error message prints out you know it’s not the upload but something later on in the script that’s causing the error 500. In the first case you talk to the web host about it. In the second, you keep on debugging until you find and solve the problem.

Where’s your form code?

Sorry for the delayed reply.


<label>PDF File</label> <input type="file" name="pdf" onchange="checkName(this, 'sname', 'check')"><br />
<input type="text" disabled="disabled" value="" name="denumire1" id="sname" style="display:none;"/>
<label>Cover Image</label> <input type="file" name="img" onchange="checkName(this, 'fname', 'check')" ><br />

<br />
 <input type="submit" id="check" name="addissue" value="Submit" disabled="disabled" />
</fieldset>
<input type="text" value="" name="denumire2" id="fname" style="display:none;"/><br />
</form>
<script type="text/javascript">
var ar_ext = ['pdf', 'gif', 'jpeg', 'jpg', 'png'];        // array with allowed extensions

function checkName(el, to, sbm) {
// - coursesweb.net
  // get the file name and split it to separe the extension
  var name = el.value;
  var ar_name = name.split('.');

  // for IE - separe dir paths (\\) from name
  var ar_nm = ar_name[0].split('\\\\');
  for(var i=0; i<ar_nm.length; i++) var nm = ar_nm[i];

  // add the name in 'to'
  document.getElementById(to).value = nm;

  // check the file extension
  var re = 0;
  for(var i=0; i<ar_ext.length; i++) {
    if(ar_ext[i] == ar_name[1]) {
      re = 1;
      break;
    }
  }

  // if re is 1, the extension is in the allowed list
  if(re==1) {
    // enable submit
    document.getElementById(sbm).disabled = false;
  }
  else {
    // delete the file name, disable Submit, Alert message
    el.value = '';
    document.getElementById(sbm).disabled = true;
    alert('".'+ ar_name[1]+ '" is not an file type allowed for upload');
  }
}
</script>

Try changing your max_execution_time in php.ini
Maybe your script is taking too long to upload bigger files

Unless you have changed this

// declare where to upload the file

     $targeta="../publication/" . $userid . "/$_GET[id]/".$random_digit."_".$_FILES['pdf']['name'];

    // move the file

    if(move_uploaded_file($_FILES['pdf']['tmp_name'],$targeta)){ 

you are moving the file before checking for errors. That could well cause the 500 error