Jump to content
Larry Ullman's Book Forums

File Uploads Script Does Not Work?


Recommended Posts

I am working on File uploads and the script does not work.

I have checked for php.ini and everything is fine.

file_uploads = on

;upload_tmp_dir =

upload_max_filesize = 2m

max_file_uploads = 20

=====================

I am using windows 7.

uploads folder is inside htdoc and the web root folder inside htdoc.

============================================================

How can I fix this issue?

Thank you

==================================================================

<h2> Upload An Image </h2>

 

<?php

# Upload An Image scripts.

if($_SERVER['REQUEST_METHOD'] == 'POST') {

// Check for an uploaded file:

if(isset($_FILE['upload'])) {

// Validate image type:

$allowed = array('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png' );

 

if(in_array($_FILES['upload']['type'], $allowed) ) {

// Move the file over:

if(move_uploaded_file($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name']}") ) {

echo '<p> <em> The file has been uploaded! </em> </p>';

 

}// End of move_uploaded_file

 

} else { // Invalid type.

echo '<p> Please upload a jpeg or png image. </p>';

}

 

 

}// End of if(isset($_FILE['upload']))

 

// Check for an error:

if($_FILES['upload']['error'] > 0) {

echo '<p> The file could not be uploaded because: <strong> ';

 

// Print a message base upon the error.

switch($_FILES['upload']['error']) {

case 1:

print 'The file exceeds the upload_max_filesize setting in php.ini';

break;

 

case 2:

print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';

break;

 

case 3:

print 'The file was only partially uploaded.';

break;

 

case 4:

print 'No file was uploaded.';

break;

 

case 6:

print 'No temporary folder was available.';

break;

 

case 7:

print 'Unable to write to the disk';

break;

 

case 8:

print 'File upload stopped.';

break;

 

default:

print 'A system error ocurred.';

break;

 

}// End of SWITCH.

print '</strong> </p>';

 

} // End of error IF.

 

// Delete the file if it still exists:

if(file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])) {

unlink($_FILES['upload']['tmp_name']);

 

}

 

} // End of the main submition if statement.

 

?>

 

<form style="margin:10px;" action="upload_image.php" method="post" enctype="multipart/form-data">

 

<input type="hidden" name="MAX_FILE_SIZE" value="524288" />

 

<fieldset>

<legend> Select a jpeg or png image of 512KB or smaller to be uploaded:

<p style="margin:10px;"> <strong>File: </strong> <input type="file" name="upload" /> </p>

</legend>

</fieldset>

<p> <input type="submit" name="submit" value="Upload" /> </p>

 

</form>

Link to comment
Share on other sites

;;;;;;;;;;;;;;;;

; File Uploads ;

;;;;;;;;;;;;;;;;

 

; Whether to allow HTTP file uploads.

; http://php.net/file-uploads

file_uploads = On

 

; Temporary directory for HTTP uploaded files (will use system default if not

; specified).

; http://php.net/upload-tmp-dir

upload_tmp_dir = "C:\xampp\tmp"

 

; Maximum allowed size for uploaded files.

; http://php.net/upload-max-filesize

upload_max_filesize = 2M

 

; Maximum number of files that can be uploaded via a single request

max_file_uploads = 20

Link to comment
Share on other sites

You will likely then need to turn your error reporting on. It is defaulted to off. What you want to do is in the php.ini file set the display_errors flag to on and then restart Apache. Try your page again and you should see an error or warning message. Note, it is mentioned several times in the books that you would only want to do this for your development server. Don't have this turned on for your live site as it can lead to security issues.

  • Upvote 1
Link to comment
Share on other sites

Even I have tried to upload a file to a live server but it is not working.

display_errors error is already on.

That is really strange !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

====================================================================

 

; http://php.net/display-errors

display_errors = On

====================================================================

; The display of errors which occur during PHP's startup sequence are handled

; separately from display_errors. PHP's default behavior is to suppress those

; errors from clients. Turning the display of startup errors on can be useful in

; debugging configuration problems. But, it's strongly recommended that you

; leave this setting off on production servers.

; Default Value: Off

; Development Value: On

; Production Value: Off

; http://php.net/display-startup-errors

display_startup_errors = On

Link to comment
Share on other sites

You have a typo in your script. The line below is incorrect.

 

// Check for an uploaded file:

if(isset($_FILE['upload'])) {

 

It should be:

 

// Check for an uploaded file:

if(isset($_FILES['upload'])) {

 

Happens to me all the time.

Link to comment
Share on other sites

  • 1 month later...

hi everyone.

 

ive done all the above, double checked my php.ini and my source code. ill paste the source code below once i explain my predicament.

 

i keep getting the default error message printing, "something unforeseen happened".

 

I dont know why this is happening and hoping that someone here my have an idea??

im using MAMP on php 5.4.4 and my OS is MAC OSX 10.7.5.

 

here is the code below, I have triple checked it but im gonna but it here just in case:

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

<title>View a quotation!</title>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<link rel="stylesheet" href="css/1.css" type="text/css" media="screen,projection" />

</head>

<body>

<?php

//script 11.4 - upload_file.php

 

/* this script displays and handles an html form. this script takes a file upload and stores it on the server */

 

if ($_SERVER['REQUEST_METHOD'] == 'POST')

{

if (move_uploaded_file ($_FILES['the_file']['tmp_file'], "../uploads/{$_FILES['the_file']['name']}"))

{

print '<p>Your file has been uploaded.</p>';

} else

{

//problem

print '<p style="color=red;">Your file could not be uploaded because: ';

 

//print a message based on the error

switch ($_FILES['the_file']['error'])

{

case 1:

print 'The file exceeds the upload_max_filesize setting in php.ini';

break;

case 2:

print 'The file max file size setting in the html form';

break;

case 3:

print 'The file was only partially uploaded';

break;

case 4:

print 'No file was uploaded';

break;

case 6:

print 'The temporary folder does not exist';

break;

 

default:

print 'Something unforeseen happened.';

break;

}

 

print '.</p>'; //completing the paragraph from above.

}//end of the move_uploaded_file() IF.

} //End of submission IF

//leave PHP and display the form

?>

 

<form action="upload_file.php" enctype="multipart/form-data" method="post">

<p>Upload a file using this form:</p>

<input type="hidden" name="MAX_FILE_SIZE" value="300000" />

<p><input type="file" name="the_file" /></p>

<p><input type="submit" name="submit" value="Upload This File"/> </p>

</form>

</body>

</html>

Link to comment
Share on other sites

thanks larry!

it seems to have confirmed that nothing is wrong tho. i got error 0.

 

Array ( [name] => uploading tester.jpeg [type] => image/jpeg [tmp_name] => /Applications/MAMP/tmp/php/phpzs45VT [error] => 0 => 5876 )

 

Ive just figured it out, line 16,

if (move_uploaded_file ($_FILES['the_file']['tmp_file'], "../uploads/{$_FILES['the_file']['name']}"))

 

it should be ['tmp_name']

 

sorry for wasting time.

Link to comment
Share on other sites

 Share

×
×
  • Create New...