hello.
i am having some problems with my upload script and i don't really know what's wrong.
i am running apache2.2 with php 5.2.6 with mysql5 on windows xp.
i am currently working on a youtube clone script.everything works great, except the uploading the file to the server.
i will give u the code for index.php

<?php
session_start();
if(!isset($_POST['upload'])) {
	echo '
		<form name="upload" enctype="multipart/form-data" method="POST" action="'.$_SERVER['REQUEST_URI'].'">
		<input type="file" name="file" size="13" value="">
		<br /><input type="submit" name="upload" value="Upload">
		</form>
	';
} 
else 
{
	$yourdomain = 'mydomain.com'; //it's set correctly in script, just removed for public view
	$uploaddir = 'upload/raw/';
	$filename = $_FILES['file']['name'];
	$filesize = $_FILES['file']['size'];
	$tmpname_file = $_FILES['file']['tmp_name'];
	move_uploaded_file($tmpname_file, "$uploaddir$filename");
	echo "Successful.<br /><b>URL: </b><textarea rows='1' cols='13'>".$yourdomain.$uploaddir.$filename."</textarea>";
}
?> 

the thing is it works with .jpg files and mp3 (as i tested) but when i try to upload a .mpg it just refresh the page and it doesn't upload the file.
the limit in php.ini for uploading files is set to 500M, the .mpg is 50M, and in phpinfo() it display that the limit is set to 500M, so i guess no problem there.
pls help.thanks.

L.E:

i succesfully uploaded a 1.6Mb .mpg and converted it to .flv using my script.
still, when i try to upload bigger .mpg files (like 48Mb and 50 Mb) it just reload the script so i guess the problem is about the file size.
i also updated the following directives in php.ini:

max_execution_time = 300000     ; Maximum execution time of each script, in seconds
max_input_time = 600000000000000000 ; Maximum amount of time each script may spend parsing request data
memory_limit = 900M      ; Maximum amount of memory a script may consume (8MB)

i guess the values i wrote are big enough.the values are also updated in phpinfo();
any ideas?

    private_click;10881775 wrote:

    hello.
    i am having some problems with my upload script and i don't really know what's wrong.
    i am running apache2.2 with php 5.2.6 with mysql5 on windows xp.
    i am currently working on a youtube clone script.everything works great, except the uploading the file to the server.
    i will give u the code for index.php

    <?php
    session_start();
    if(!isset($_POST['upload'])) {
    	echo '
    		<form name="upload" enctype="multipart/form-data" method="POST" action="'.$_SERVER['REQUEST_URI'].'">
    		<input type="file" name="file" size="13" value="">
    		<br /><input type="submit" name="upload" value="Upload">
    		</form>
    	';
    } 
    else 
    {
    	$yourdomain = 'mydomain.com'; //it's set correctly in script, just removed for public view
    	$uploaddir = 'upload/raw/';
    	$filename = $_FILES['file']['name'];
    	$filesize = $_FILES['file']['size'];
    	$tmpname_file = $_FILES['file']['tmp_name'];
    	move_uploaded_file($tmpname_file, "$uploaddir$filename");
    	echo "Successful.<br /><b>URL: </b><textarea rows='1' cols='13'>".$yourdomain.$uploaddir.$filename."</textarea>";
    }
    ?> 

    the thing is it works with .jpg files and mp3 (as i tested) but when i try to upload a .mpg it just refresh the page and it doesn't upload the file.
    the limit in php.ini for uploading files is set to 500M, the .mpg is 50M, and in phpinfo() it display that the limit is set to 500M, so i guess no problem there.
    pls help.thanks.

    L.E:

    i succesfully uploaded a 1.6Mb .mpg and converted it to .flv using my script.
    still, when i try to upload bigger .mpg files (like 48Mb and 50 Mb) it just reload the script so i guess the problem is about the file size.
    i also updated the following directives in php.ini:

    max_execution_time = 300000     ; Maximum execution time of each script, in seconds
    max_input_time = 600000000000000000 ; Maximum amount of time each script may spend parsing request data
    memory_limit = 900M      ; Maximum amount of memory a script may consume (8MB)

    i guess the values i wrote are big enough.the values are also updated in phpinfo();
    any ideas?

    apache should have a size limiter in it as well. you'll have to rumage throught the http-conf file but its in there..

    hope that helps

      There are several factors that must be considered when uploading files with PHP. I suggest that you go back and reexamine the relevant manual page:

      http://us3.php.net/manual/en/features.file-upload.php

      1.) You didn't include the hidden form element MAX_FILE_SIZE.
      2.) You didn't explicitly specify an upload_max_filesize in php.ini.

      There are two php.ini directives with which you should concern yourself, in addition to those noted in your original post:

      post_max_size
      upload_max_filesize

      Try the following:

      File upload class example (save as upload.class.php):

      <?php
      
      class Upload
      {
      	//Handles a file upload using PHP's built-in functions.
      	//Returns an array that contains a boolean value (that represents success or failure)
      	//PHP's error code (which is 0 if no error), and an error message (if the error code is not 0) or the moved file's
      	//new location and name (if the error code is 0).
      	function processUpload($tmpFileName)
      	{
      		//First, we have to make sure that the $_FILES superglobal is actually populated.
      		if (count($_FILES) > 0) {
      			//Next, we'll check to see if PHP reported any problems with the file.
      
      		//Let's capture the error code (even if it's 0), so we can
      		//include the error code in the function's return value.
      		$returnArray['errcode'] = $_FILES['uploadedFile']['error'];
      
      		//If the "error" key in the $_FILES superglobal equals 0, PHP didn't detect any problems
      		//with the upload, and we can skip ahead. If not, we need to do some more work.		
      		if ($_FILES['uploadedFile']['error'] != 0) {
      			switch ($_FILES['uploadedFile']['error']) {
      			//First, let's address issues over which the user has no control.
      			//(These messages were taken directly from the PHP Manual.)
      			case 1:
      				$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
      				break;
      			case 2:
      				$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
      				break;
      			case 6:
      				$error = 'Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.';
      				break;
      			case 7:
      				$error = 'Failed to write file to disk. Introduced in PHP 5.1.0.';
      				break;
      			case 8:
      				$error = 'File upload stopped by extension. Introduced in PHP 5.2.0.';
      				break;
      			//And now for problems the user is able to help assauge.
      			case 3:
      				$error = 'The uploaded file was only partially uploaded.';
      				break;
      			case 4:
      				$error = 'No file was uploaded.';
      				break;
      			}
      
      			if (isset($error)) {
      				$returnArray['result'] = FALSE;
      				$returnArray['message'] = $error;
      			}
      			else {
      				$returnArray['result'] = TRUE;
      			}			
      		}
      		else {
      			$returnArray['result'] = TRUE;
      		}
      
      		//If none of the above errors prevented the upload
      		//from at least landing in PHP's tmp (temporary) directory,
      		//let's see what the file's name is.
      		if (isset($_FILES['uploadedFile']['tmp_name']) && $_FILES['uploadedFile']['tmp_name'] != '') {
      			$returnArray['tmpName'] = $_FILES['uploadedFile']['tmp_name'];
      		}
      	}
      	else {
      		$returnArray['result'] = FALSE;
      		$returnArray['message'] = 'PHP\'s $_FILES superglobal is not populated for some reason. The upload cannot be processed.';
      	}
      
      	return $returnArray;
      }
      
      //Moves a file (provided a fully-qualified path and file name) from
      //its temporary location (after a PHP-handled POST/PUT file upload)
      //to a more permanent location, e.g., on a Web server's hard disk drive.
      function moveUploadedFile($tmpFile, $newDestination)
      {
      	if (@file_exists($tmpFile)) {
      		$res = move_uploaded_file($tmpFile, $newDestination);
      
      		if ($res !== FALSE) {
      			return TRUE;
      		}
      		else {
      			return FALSE;
      		}
      	}
      	else {
      		return FALSE;
      	}
      }
      }
      

      PHP business logic and HTML form:

      <?php
      
      //Require the necessary upload class file.
      require('upload.class.php');
      
      //We only want to do this stuff if a file has been uploaded via the POST method.
      if (count($_POST) > 0) {
      	//Attempt to set the php.ini value that governs how long
      	//"temporary" data is allowed to exist before PHP's "garbage collection"
      	//process destroys it.
      	//
      	//The default value is only 24 minutes. We may need more like an hour here.
      	@ini_set("session.gc_maxlifetime","3600");
      
      //Let's attempt to validate the file upload.
      $res = Upload::processUpload('uploadedFile');
      
      if ($res['result'] === TRUE) {
      	//The file made it into PHP's temporary directory without issues. Nice.
      
      	//Next, let's try to move the file to another location, so PHP doesn't
      	//automatically delete the file at the end of script execution.
      	$sourceFile = $res['tmpName'];
      	$targetFile = '/some/location/on/your/filesystem/file.txt';
      	$res = Upload::moveUploadedFile($sourceFile, $targetFile);
      
      	if ($res === TRUE) {
      		//The file was uploaded AND moved successfully.
      		//That's it!
      	}
      	else {
      		die('The file was uploaded successfully, but could not be moved out of PHP\'s temporary directory.');
      	}
      }
      else {
      	die('The file upload could not be processed because: ' . $res['message']);
      }
      }
      else {
      	die('No file was uploaded! Did you choose a file before clicking &quot;Upload File&quot;?');
      }
      
      ?>
      
      <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
      		<!-- MAX_FILE_SIZE must precede the file input field. -->
      		<input type="hidden" name="MAX_FILE_SIZE" value="650000000" />
      		<!-- Name of input element determines name in PHP's $_FILES array. -->
      		<input name="uploadedFile" type="file" />
      		<input type="submit" value="Upload File" />
      </form>
      

      Let us know if you are still stuck.

        the problem was at the "post_max_size" directive, it was set to 8M.everything works great.
        thanx for your help cbj4074.

          Glad I could help!

          Mark the thread resolved, please.

            Write a Reply...