code works on remote host but not localhost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
attaboy
Forum Newbie
Posts: 13
Joined: Wed Apr 10, 2013 7:09 pm

code works on remote host but not localhost

Post by attaboy »

I have code to upload mp3 to a server when run from my godaddy account it works fine when I run locally I get "invalid file" which is a message generated from the script. I looked through my php.ini file amd dindn't find anything, maybe I just don't know what to look for. I'm sure there must be a setting somewhere in some configuration file that would fix my problem.
here is the code

Code: Select all

<!doctype html>
<html>
<head>
<title> upload songs </title>
</head>
<body>
<form action="upload_songs.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" id="submit" value="Submit">
</form>


<?php
if (isset($_POST["submit"])) {
  $path = './upload/';
  $maxFileSize = 1 * (1024 * 1024 * 20);     // 20Mb
  $allowedExts = array("mp3", "wma", "aif");
  $allowedMimes = array("audio/mp3", "audio/mpeg", "audio/x-ms-wma", "audio/x-aiff");
//  $extension = end(explode(".", $_FILES["file"]["name"])); this generates warning pathinfo doesn't
  $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  if (($_FILES["file"]["size"] < $maxFileSize) && in_array($_FILES["file"]["type"], $allowedMimes) && in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";


      if (file_exists($path . $_FILES["file"]["name"])) {
        echo $_FILES["file"]["name"] . " already exists. ";
      } else {
        move_uploaded_file($_FILES["file"]["tmp_name"], $path . $_FILES["file"]["name"]);
        echo "Stored in: $path" . $_FILES["file"]["name"];
      }
    }
  } else {
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Invalid file<br/>";
    echo '<pre>' . print_r($_FILES) . '</pre>';
  }
}
?>
</body>
</html>
Thanks in advance for any ideas.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: code works on remote host but not localhost

Post by requinix »

What did that print_r() you have there output?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: code works on remote host but not localhost

Post by Christopher »

In the last thread I told you to split this if() into three nested if()s so you could have individual error message for the three checks:

Code: Select all

  if (($_FILES["file"]["size"] < $maxFileSize) && in_array($_FILES["file"]["type"], $allowedMimes) && in_array($extension, $allowedExts)) {
(#10850)
attaboy
Forum Newbie
Posts: 13
Joined: Wed Apr 10, 2013 7:09 pm

Re: code works on remote host but not localhost

Post by attaboy »

I made the changes Christopher requested plus added 3 lines of code before that to show the file size, type and extension name. The With mp3 on my localhost the size is 0 the type is blank and the extension is mp3
this doesn't happen when I upload wma or when I upload on my hosted site on godaddy that's why I think there's configuration setting I need to set to allow mp3's to POST on my home pc.

this is the code as is(a bit messy at this point)

Code: Select all

<?php
if (isset($_POST["submit"])) {
  $path = './../upload/';
  $maxFileSize = 1 * (1024 * 1024 * 20);     // 20Mb
  $allowedExts = array("mp3", "wma", "aif");
  $allowedMimes = array("audio/mp3", "audio/mpeg", "audio/x-ms-wma", "audio/x-aiff");
//  $extension = end(explode(".", $_FILES["file"]["name"])); this generates warning pathinfo doesn't
  $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
  
  echo $_FILES["file"]["size"]." test1<br>";
  echo $_FILES["file"]["type"]." test2<br>";
  echo $extension." test3<br>";
  
//  if (($_FILES["file"]["size"] < $maxFileSize) && in_array($_FILES["file"]["type"], $allowedMimes) && in_array($extension, $allowedExts)) {
	if($_FILES["file"]["size"] < $maxFileSize) {
		if ($_FILES["file"]["error"] > 0) {
		  echo "Size: " . $_FILES["file"]["type"] . "<br>";
		  echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
		} 
	}	
	if (in_array($_FILES["file"]["type"], $allowedMimes)) {
		if ($_FILES["file"]["error"] > 0) {
		  echo "Type: " . $_FILES["file"]["type"] . "<br>";
		  echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
		} 		
	}
	if(in_array($extension, $allowedExts)) {
		if ($_FILES["file"]["error"] > 0) {
		  echo $extension . "<br>";
		  echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
		} 		
	}
	if($_FILES["file"]["error"] == 0) {		
	  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
	  echo "Type: " . $_FILES["file"]["type"] . "<br>";
	  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
	  echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

	  if (file_exists($path . $_FILES["file"]["name"])) {
		echo $_FILES["file"]["name"] . " already exists. ";
	  } else {
		move_uploaded_file($_FILES["file"]["tmp_name"], $path . $_FILES["file"]["name"]);
		echo "Stored in: $path" . $_FILES["file"]["name"];
	  }
	} else {
	echo "Type: " . $_FILES["file"]["type"] . "<br>";
	echo "Invalid file<br/>";
	echo '<pre>' . print_r($_FILES) . '</pre>';
  }
}

?>
This is the output:

0 test1
test2
mp3 test3
Size:
Return Code: 1
mp3
Return Code: 1
Type:
Invalid file
Array ( [file] => Array ( [name] => 11.mp3 [type] => [tmp_name] => [error] => 1 [size] => 0 ) )
1
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: code works on remote host but not localhost

Post by requinix »

Code: Select all

Return Code: 1
File upload errors
UPLOAD_ERR_INI_SIZE
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
Post Reply