Solved

php upload script

Posted on 2009-04-02
4
408 Views
Last Modified: 2012-06-21
Hello EE
HEllo RAY

I have an error into your code
 if i dont switch  overwrite Check box and the file dont exist
the script send me this error also if work fine:

Notice: Undefined index: overwrite in /home/xxx/public_html/admin/upload.php on line 106

can you have a look? please?

Regards,
Denis

 
<?php // RAY_upload_example.php

error_reporting(E_ALL);

 

// MANUAL REFERENCE PAGES

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

// http://docs.php.net/manual/en/features.file-upload.common-pitfalls.php

// http://docs.php.net/manual/en/function.move-uploaded-file.php

// http://docs.php.net/manual/en/function.getimagesize.php

 

// ESTABLISH THE NAME OF THE 'uploads' DIRECTORY

$uploads = 'uploads';

 

// ESTABLISH THE BIGGEST FILE SIZE WE CAN ACCEPT

$max_file_size = '8192000';  // EIGHT MEGABYTE LIMIT ON UPLOADS

 

// ESTABLISH THE KINDS OF FILE EXTENSIONS WE CAN ACCEPT

$file_exts = array('jpg', 'gif', 'png', 'txt');

 

// ESTABLISH THE NUMBER OF FILES WE CAN UPLOAD

$nf = 5;

 

 

 

// THIS IS A LIST OF THE POSSIBLE ERRORS THAT CAN BE REPORTED in $_FILES[]["error"]

$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",

	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 a file</h2>

 

	<!--

		SOME THINGS TO NOTE ABOUT THIS FORM...

		NOTE THE CHOICE OF 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

	-->

 

	<form name="UploadForm" enctype="multipart/form-data" action="<?=$_SERVER["REQUEST_URI"]?>" method="POST">

	<input type="hidden" name="p" value="1" />

	<input type="hidden" name="MAX_FILE_SIZE" value="<?=$max_file_size?>" />

	<p>

	Find the file(s) you want to upload and click the "Upload" button below.

	</p>

 

	<?php 

	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 <b>overwrite</b> existing files.

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

	</form>

	<?php

	die();

}

 

 

 

else // WE HAVE GOT SOMETHING IN $_POST

{

 

// 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() . '/' . $uploads . '/' . $f_name .'.'. $f_type;

		$my_file	= $uploads . '/' . $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");

		}

 

// MOVE THE FILE INTO THE DIRECTORY

		$overwrite	= $_POST['overwrite'];

		$file_size	= number_format($my_uploaded_file["size"]);

 

// IF THE FILE IS NEW

		if (!file_exists($my_new_file))

		{

			if (move_uploaded_file($my_uploaded_file['tmp_name'], $my_new_file))

			{

				$upload_success = 1;

			}

			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($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/><b>Attempted Backup Failed!</b>\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/><b><i>$my_file</i></b> has been saved.\n"; }

		if ($upload_success == 0) { echo "<br/><b>It was NOT overwritten.</b>\n"; }

		if ($upload_success < 0)  { echo "<br/><b>ERROR <i>$my_file</i> NOT SAVED - SEE WARNING FROM move_uploaded_file() COMMAND</b>\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 href=\"$my_file\">See the file $my_file</a>\n";

		}

// END ITERATOR

	}

}

?>

Open in new window

0
Comment
Question by:denny3d
4 Comments
 
LVL 3

Expert Comment

by:Ruurtjan
ID: 24051931
It seems that your form doesn't have a field with the name 'overwrite'.
0
 

Author Comment

by:denny3d
ID: 24051977
no, it's the form
	<h2>Upload a file</h2>

 

	<!--

		SOME THINGS TO NOTE ABOUT THIS FORM...

		NOTE THE CHOICE OF 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

	-->

 

	<form name="UploadForm" enctype="multipart/form-data" action="/admin/uploadRAY.php" method="POST">

	<input type="hidden" name="p" value="1" />

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

	<p>

	Find the file(s) you want to upload and click the "Upload" button below.

	</p>

 

	<input name="userfile0" type="file" size="80" /><br/>

<input name="userfile1" type="file" size="80" /><br/>

<input name="userfile2" type="file" size="80" /><br/>

<input name="userfile3" type="file" size="80" /><br/>

<input name="userfile4" type="file" size="80" /><br/>

	<br/>Check this box <input autocomplete="off" type="checkbox" name="overwrite" /> to <b>overwrite</b> existing files.

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

	</form>

	

Open in new window

0
 
LVL 49

Accepted Solution

by:
Roonaan earned 50 total points
ID: 24052135
Change:
$overwrite      = $_POST['overwrite'];
To
$overwrite      = isset($_POST['overwrite']) ? $_POST['overwrite'] : false;

0
 
LVL 9

Expert Comment

by:pmessana
ID: 24052632
Not stomping on an answer so don't award me anything...but Roonaan's answer is correct and teaching a man to fish is more fun.

The reason for his answer is that if the checkbox is not checked then the POST data has no value.  His is checking if it is set and if not, set it to false.

Cheers!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now