edvinson
asked on
How to append PHP script.
I would like to have this simple script tell me when upload has been successful.
<form name="uploader" method="post" action="" enctype="multipart/form-data">
<input type="file" name="image" style="width:300px;cursor:pointer" />
<input type="submit" name="upload" value="Upload Image" />
<?php
if($_POST['upload'])
{
# edit #
$maxwidth = 25000;
$maxheight = 25000;
$max_filesize = 1024000;
$uploads = 'uploads';
$types_array = array('image/gif','image/jpeg','image/x-png', 'image/jpg');
# end edit #
if($_FILES['image']['name'] == "")
{
echo "Please select a file to upload!\n";
exit;
}
if(!in_array($_FILES['image']['type'], $types_array))
{
echo "That file type is not allowed!\n";
exit;
}
$max_filesize_kb = ($max_filesize / 102400);
if($_FILES['image']['size'] > $max_filesize)
{
echo "Your file is too large, files may be up to ".$max_filesize_kb."kb\n";
exit;
}
$imagesize = getimagesize($_FILES['image']['tmp_name']);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
if($imagewidth > $maxwidth || $imageheight > $maxheight)
{
echo "You file is too large, files may be up to ".$maxwidth."px x ".$maxheight."px in size\n";
exit;
}
move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name'])
or die ("Couldn't upload ".$_FILES['image']['name']."\n");
}
?>
</form>
The move_uploaded_file() function returns a BOOLEAN (true/false) on the success of the operation. Just wrap this in an if statement:
$success = move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name'])
if ($success) {
echo "File uploaded OK";
} else {
echo "File couldn't be uploaded";
}
ASKER
Plusone3055,
I tried as you suggested, see code snippet.
It didn't work, sorry.
I tried as you suggested, see code snippet.
It didn't work, sorry.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<form name="uploader" method="post" action="" enctype="multipart/form-data">
<input type="file" name="image" style="width:300px;cursor:pointer" />
<input type="submit" name="upload" value="Upload Image" />
<?php
if($_POST['upload'])
{
# edit #
$maxwidth = 25000;
$maxheight = 25000;
$max_filesize = 1024000;
$uploads = 'uploads';
$types_array = array('image/gif','image/jpeg','image/x-png', 'image/jpg');
# end edit #
if($_FILES['image']['name'] == "")
{
echo "Please select a file to upload!\n";
exit;
}
if(!in_array($_FILES['image']['type'], $types_array))
{
echo "That file type is not allowed!\n";
exit;
}
$max_filesize_kb = ($max_filesize / 102400);
if($_FILES['image']['size'] > $max_filesize)
{
echo "Your file is too large, files may be up to ".$max_filesize_kb."kb\n";
exit;
}
$imagesize = getimagesize($_FILES['image']['tmp_name']);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
if($imagewidth > $maxwidth || $imageheight > $maxheight)
{
echo "You file is too large, files may be up to ".$maxwidth."px x ".$maxheight."px in size\n";
exit;
}
move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name'])
or die ("Couldn't upload ".$_FILES['image']['name']."\n");
}
//added this script
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "File ". $_FILES['userfile']['name'] ." uploaded successfully.\n";
echo "Displaying contents\n";
readfile($_FILES['userfile']['tmp_name']);
} else {
echo "Possible file upload attack: ";
echo "filename '". $_FILES['userfile']['tmp_name'] . "'.";
}
?>
</form>
</body>
</html>
ASKER
Chris Stanyon,
I tried yours and I get a page error: Parse error: syntax error, unexpected T_IF in /hermes/bosweb/web199/b199 2/ipw.edvi nson/publi c_html/upl oad3.php on line 63
Code attached
I tried yours and I get a page error: Parse error: syntax error, unexpected T_IF in /hermes/bosweb/web199/b199
Code attached
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<form name="uploader" method="post" action="" enctype="multipart/form-data">
<input type="file" name="image" style="width:300px;cursor:pointer" />
<input type="submit" name="upload" value="Upload Image" />
<?php
if($_POST['upload'])
{
# edit #
$maxwidth = 25000;
$maxheight = 25000;
$max_filesize = 1024000;
$uploads = 'uploads';
$types_array = array('image/gif','image/jpeg','image/x-png', 'image/jpg');
# end edit #
if($_FILES['image']['name'] == "")
{
echo "Please select a file to upload!\n";
exit;
}
if(!in_array($_FILES['image']['type'], $types_array))
{
echo "That file type is not allowed!\n";
exit;
}
$max_filesize_kb = ($max_filesize / 102400);
if($_FILES['image']['size'] > $max_filesize)
{
echo "Your file is too large, files may be up to ".$max_filesize_kb."kb\n";
exit;
}
$imagesize = getimagesize($_FILES['image']['tmp_name']);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
if($imagewidth > $maxwidth || $imageheight > $maxheight)
{
echo "You file is too large, files may be up to ".$maxwidth."px x ".$maxheight."px in size\n";
exit;
}
move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name'])
or die ("Couldn't upload ".$_FILES['image']['name']."\n");
}
//added this script
$success = move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name'])
if ($success) {
echo "File uploaded OK";
} else {
echo "File couldn't be uploaded";
}
?>
</form>
</body>
</html>
Sorry - missing semi-colon on the end of line 62:
$success = move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name']);
if ($success) {
echo "File uploaded OK";
} else {
echo "File couldn't be uploaded";
}
ASKER
thanks Chris, but not making much progress yet.
Now I have the form back but the message on screen says File couldn't Be Uploaded even before I Browse.
And it won't upload anything now.
Code attached
Now I have the form back but the message on screen says File couldn't Be Uploaded even before I Browse.
And it won't upload anything now.
Code attached
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<form name="uploader" method="post" action="" enctype="multipart/form-data">
<input type="file" name="image" style="width:300px;cursor:pointer" />
<input type="submit" name="upload" value="Upload Image" />
<?php
if($_POST['upload'])
{
# edit #
$maxwidth = 25000;
$maxheight = 25000;
$max_filesize = 1024000;
$uploads = 'uploads';
$types_array = array('image/gif','image/jpeg','image/x-png', 'image/jpg');
# end edit #
if($_FILES['image']['name'] == "")
{
echo "Please select a file to upload!\n";
exit;
}
if(!in_array($_FILES['image']['type'], $types_array))
{
echo "That file type is not allowed!\n";
exit;
}
$max_filesize_kb = ($max_filesize / 102400);
if($_FILES['image']['size'] > $max_filesize)
{
echo "Your file is too large, files may be up to ".$max_filesize_kb."kb\n";
exit;
}
$imagesize = getimagesize($_FILES['image']['tmp_name']);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
if($imagewidth > $maxwidth || $imageheight > $maxheight)
{
echo "You file is too large, files may be up to ".$maxwidth."px x ".$maxheight."px in size\n";
exit;
}
move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name'])
or die ("Couldn't upload ".$_FILES['image']['name']."\n");
}
//added this script
$success = move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name']);
if ($success) {
echo "File uploaded OK";
} else {
echo "File couldn't be uploaded";
}
?>
</form>
</body>
</html>
Didn't we just go over this question recently? Or maybe it was a different question. Here is the starting point for understanding file uploads. Please read it over -- code, comments and especially the man pages, and post back if it does not answer your questions, thanks.
<?php // RAY_upload_example.php
error_reporting(E_ALL);
// MANUAL REFERENCE PAGES YOU MUST UNDERSTAND TO UPLOAD FILES
// http://php.net/manual/en/features.file-upload.php
// http://php.net/manual/en/features.file-upload.common-pitfalls.php
// http://php.net/manual/en/function.move-uploaded-file.php
// http://php.net/manual/en/reserved.variables.files.php
// MANUAL PAGES THAT ARE IMPORTANT IF YOU ARE DEALING WITH LARGE FILES
// http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
// http://php.net/manual/en/ini.core.php#ini.post-max-size
// http://php.net/manual/en/info.configuration.php#ini.max-input-time
// PHP 5.1+ SEE http://php.net/manual/en/function.date-default-timezone-set.php
date_default_timezone_set('America/Chicago');
// ESTABLISH THE NAME OF THE DESTINATION FOLDER ('uploads' DIRECTORY)
$uploads = 'RAY_junk';
if (!is_dir($uploads))
{
mkdir($uploads);
}
// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT - ABOUT 8 MB
$max_file_size = '8000000';
// ESTABLISH THE MAXIMUM NUMBER OF FILES WE CAN UPLOAD
$nf = 3;
// ESTABLISH THE KINDS OF FILE EXTENSIONS WE CAN ACCEPT
$file_exts = array
( 'jpg'
, 'gif'
, 'png'
, 'txt'
, 'pdf'
)
;
// LIST OF THE ERRORS THAT MAY BE REPORTED IN $_FILES[]["error"] (THERE IS NO #5)
$errors = array
( 0 => "Success!"
, 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini"
, 2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"
, 3 => "The uploaded file was only partially uploaded"
, 4 => "No file was uploaded"
, 5 => "UNDEFINED ERROR"
, 6 => "Missing a temporary folder"
, 7 => "Cannot write file to disk"
)
;
// IF THERE IS NOTHING IN $_POST, PUT UP THE FORM FOR INPUT
if (empty($_POST))
{
?>
<h2>Upload <?php echo $nf; ?> file(s)</h2>
<!--
SOME THINGS TO NOTE ABOUT THIS FORM...
ENCTYPE IN THE HTML <FORM> STATEMENT
MAX_FILE_SIZE MUST PRECEDE THE FILE INPUT FIELD
INPUT NAME= IN TYPE=FILE DETERMINES THE NAME YOU FIND IN $_FILES ARRAY
ABSENCE OF ACTION= ATTRIBUTE IN FORM TAG CAUSES POST TO SAME URL
-->
<form name="UploadForm" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
<p>
Find the file(s) you want to upload and click the "Upload" button below.
</p>
<?php // CREATE INPUT STATEMENTS FOR UP TO $n FILE NAMES
for ($n = 0; $n < $nf; $n++)
{
echo "<input name=\"userfile$n\" type=\"file\" size=\"80\" /><br/>\n";
}
?>
<br/>Check this box <input autocomplete="off" type="checkbox" name="overwrite" /> to <strong>overwrite</strong> existing files.
<input type="submit" value="Upload" />
</form>
<?php
die();
}
// END OF THE FORM SCRIPT
// WE HAVE GOT SOMETHING IN $_POST - RUN THE ACTION SCRIPT
else
{
// THERE IS POST DATA - PROCESS IT
echo "<h2>Results: File Upload</h2>\n";
// ACTIVATE THIS TO SEE WHAT IS COMING THROUGH
// echo "<pre>"; var_dump($_FILES); var_dump($_POST); echo "</pre>\n";
// ITERATE OVER THE CONTENTS OF $_FILES
foreach ($_FILES as $my_uploaded_file)
{
// SKIP OVER EMPTY SPOTS - NOTHING UPLOADED
$error_code = $my_uploaded_file["error"];
if ($error_code == 4) continue;
// SYNTHESIZE THE NEW FILE NAME
$f_type = trim(strtolower(end (explode( '.', basename($my_uploaded_file['name'] )))));
$f_name = trim(strtolower(current(explode( '.', basename($my_uploaded_file['name'] )))));
$my_new_file
= getcwd()
. DIRECTORY_SEPARATOR
. $uploads
. DIRECTORY_SEPARATOR
. $f_name
. '.'
. $f_type
;
$my_file
= $uploads
. DIRECTORY_SEPARATOR
. $f_name
. '.'
. $f_type;
// OPTIONAL TEST FOR ALLOWABLE EXTENSIONS
if (!in_array($f_type, $file_exts)) die("Sorry, $f_type files not allowed");
// IF THERE ARE ERRORS
if ($error_code != 0)
{
$error_message = $errors[$error_code];
die("Sorry, Upload Error Code: $error_code: $error_message");
}
// GET THE FILE SIZE
$file_size = number_format($my_uploaded_file["size"]);
// IF THE FILE IS NEW (DOES NOT EXIST)
if (!file_exists($my_new_file))
{
// IF THE MOVE FUNCTION WORKED CORRECTLY
if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
{
$upload_success = 1;
}
// IF THE MOVE FUNCTION FAILED
else
{
$upload_success = -1;
}
}
// IF THE FILE ALREADY EXISTS
else
{
echo "<br/><b><i>$my_file</i></b> already exists.\n";
// SHOULD WE OVERWRITE THE FILE? IF NOT
if (empty($_POST["overwrite"]))
{
$upload_success = 0;
}
// IF WE SHOULD OVERWRITE THE FILE, TRY TO MAKE A BACKUP
else
{
$now = date('Y-m-d');
$my_bak = $my_new_file . '.' . $now . '.bak';
if (!copy($my_new_file, $my_bak))
{
echo "<br/><strong>Attempted Backup Failed!</strong>\n";
}
if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))
{
$upload_success = 2;
}
else
{
$upload_success = -1;
}
}
}
// REPORT OUR SUCCESS OR FAILURE
if ($upload_success == 2) { echo "<br/>It has been overwritten.\n"; }
if ($upload_success == 1) { echo "<br/><strong>$my_file</strong> has been saved.\n"; }
if ($upload_success == 0) { echo "<br/><strong>It was NOT overwritten.</strong>\n"; }
if ($upload_success < 0) { echo "<br/><strong>ERROR: $my_file NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND</strong>\n"; }
if ($upload_success > 0)
{
echo "$file_size bytes uploaded.\n";
if (!chmod ($my_new_file, 0755))
{
echo "<br/>chmod(0755) FAILED: fileperms() = ";
echo substr(sprintf('%o', fileperms($my_new_file)), -4);
}
echo "<br/><a target=\"_blank\" href=\"$my_file\">See the file $my_file</a>\n";
}
// END FOREACH ITERATOR - EACH ITERATION PROCESSES ONE FILE
}
}
Right. Maybe I should have been clearer. The code I posted was to replace your existing move_uploaded_file() function. So instead of this:
move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name'])
or die ("Couldn't upload ".$_FILES['image']['name']."\n");
you have this:$success = move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name']);
if ($success) {
echo "File uploaded OK";
} else {
echo "File couldn't be uploaded";
}
ASKER
Chris, Please see http://www.venicemediagroup.com/upload3.php
Script is working now, but can't get rid of the line that says File Couldn't Be Uploaded.
Script is working now, but can't get rid of the line that says File Couldn't Be Uploaded.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<form name="uploader" method="post" action="" enctype="multipart/form-data">
<input type="file" name="image" style="width:300px;cursor:pointer" />
<input type="submit" name="upload" value="Upload Image" />
<?php
if($_POST['upload'])
{
# edit #
$maxwidth = 25000;
$maxheight = 25000;
$max_filesize = 1024000;
$uploads = 'uploads';
$types_array = array('image/gif','image/jpeg','image/x-png', 'image/jpg');
# end edit #
if($_FILES['image']['name'] == "")
{
echo "Please select a file to upload!\n";
exit;
}
if(!in_array($_FILES['image']['type'], $types_array))
{
echo "That file type is not allowed!\n";
exit;
}
$max_filesize_kb = ($max_filesize / 102400);
if($_FILES['image']['size'] > $max_filesize)
{
echo "Your file is too large, files may be up to ".$max_filesize_kb."kb\n";
exit;
}
$imagesize = getimagesize($_FILES['image']['tmp_name']);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
if($imagewidth > $maxwidth || $imageheight > $maxheight)
{
echo "You file is too large, files may be up to ".$maxwidth."px x ".$maxheight."px in size\n";
exit;
}
//move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name'])
//or die ("Couldn't upload ".$_FILES['image']['name']."\n");
}
//added this script
$success = move_uploaded_file($_FILES['image']['tmp_name'], $uploads.'/'.$_FILES['image']['name']);
if ($success) {
echo "File uploaded OK";
} else {
echo "File couldn't be uploaded";
}
?>
</form>
</body>
</html>
ASKER
Ray, Thank you. I had tried your script, and it was nice. I just needed for this gig a simple one line, one box uploader. This one is working out as soon as we get the perpetual File Couldn't Be Uploaded line off the page.
A simple one line, one box uploader may be easier to find if you'll let us help you understand the principles of file uploads. There are many things that can go wrong and many moving parts in the process. If you write a few extra lines of code, you may be able to visualize the issues. That will be faster than guessing why it keeps saying File Couldn't Be Uploaded.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
YES!! that's it, thank you. GREAT JOB!
I keep up my subscription here on Experts because I want to come here for solid answers and when I need them fast.
I pay monthly here and I also pay monthly for Lynda.com.
I come here for DIRECT ANSWERS and let the answers tutor me after the fact, but I go to Lynda to be taught when I have the time to sit through the lessons. My job won't allow for as much of that as I would like.
Thank you Experts for the ANSWERS and I'm sorry if I don't come here to be tutored and if you feel that's what the mission of this site is.
I'm pretty sure you don't have the time or patience for me when it comes to that.
I keep up my subscription here on Experts because I want to come here for solid answers and when I need them fast.
I pay monthly here and I also pay monthly for Lynda.com.
I come here for DIRECT ANSWERS and let the answers tutor me after the fact, but I go to Lynda to be taught when I have the time to sit through the lessons. My job won't allow for as much of that as I would like.
Thank you Experts for the ANSWERS and I'm sorry if I don't come here to be tutored and if you feel that's what the mission of this site is.
I'm pretty sure you don't have the time or patience for me when it comes to that.
http://php.net/manual/en/function.is-uploaded-file.php