Jump to content

PHP File Upload


Altec

Recommended Posts

I'm currently writing a gallery with PHP and MySQL and I'm having a little trouble with uploading the photo. Here is what I have right now:

<?php
switch ($_GET['page']) {
case 'process':
	$_FILES["uploaded_file"] = $_POST["uploadedfile"];
	if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
		$filename = basename($_FILES['uploaded_file']['name']);
		$ext = substr($filename, strrpos($filename, '.') + 1);
		if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 2097152)) {
			$newname = '/home/phreakyo/public_html/playground/gallery/photos/'.$filename;
			if (!file_exists($newname)) {
				if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
					echo 'The photo has been saved as: '.$newname;
					$_SESSION['photo_filename'] = $filename;
				} else {
					echo 'Error: A problem occurred during file upload.';
				}
			} else {
				echo 'Error: File '.$_FILES["uploaded_file"]["name"].' already exists.';
			}
		} else {
			echo 'Error: Only .JPG images under 2 megabytes are accepted for upload.';
		}
	} else {
		echo 'Error: No file uploaded.';
	}
break;
default:
?>
<p>Use this form the upload a photo. All uploaded photos are saved to "/photos/."</p>
<form enctype="multipart/form-data" action="add_new_photo.php?page=process" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
Choose a photo to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload Photo" />
</form>	
<?php
break;
}
?>

Every time I upload a photo I get "Error: No file uploaded" which means that the file doesn't get uploaded. This is my first attempt at uploading files with PHP, so I'm sure the answer is pretty obvious. :P Anyone?

Link to comment
Share on other sites

The only reason I have the MAX_FILE_UPLOAD size as a hidden form field is because most browsers will catch it. It is by no means an attempt at setting a limit; PHP has an upload limit which is two megabytes. If you didn't notice, the limit (2097152 bytes) is coded into the script itself.

 

@toplay: Basically I just have to change all instances of $_FILES["uploaded_file"] to $_FILES["uploadedfile"], yes?

Link to comment
Share on other sites

You might want to rename the file if it exists, instead of throwing an error, also, the FILENAME should just be a variable from the beginning, and not a $_POST var. Using In_Array for filetype is more efficient and more flexible, with easy adding and removing file types.

 

Search those functions up.

Link to comment
Share on other sites

Thanks for all the help. Here is what I have so far:

<?php
switch ($_GET['page']) {
case 'process':
	if((!empty($_FILES['uploadedfile'])) && ($_FILES['uploadedfile']['error'] == 0)) {
		$filename = basename($_FILES['uploadedfile']['name']);
		$path = pathinfo($_SERVER['SCRIPT_FILENAME']); $serv_path = str_replace('admin', 'photos', $path['dirname']);
		$ext = substr($filename, strrpos($filename, '.') + 1);
		if (($ext == 'jpg') && ($_FILES['uploadedfile']['type'] == 'image/jpeg') && ($_FILES['uploadedfile']['size'] < 2097152)) {
			$newname = $serv_path.'/'.$filename;
			if (!file_exists($newname)) {
				if ((move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$newname))) {
					echo 'The photo has been saved as: '.$newname;
					$_SESSION['photo_filename'] = $filename;
				} else {
					echo 'Error: A problem occurred during file upload.';
				}
			} else {
				echo 'Error: File '.$_FILES["uploadedfile"]["name"].' already exists.';
			}
		} else {
			echo 'Error: Only .JPG images under 2 megabytes are accepted for upload.';
		}
	} else {
		echo 'Error: No file uploaded.';
	}
break;
default:
?>
<p>Use this form to upload a photo.</p>
<form enctype="multipart/form-data" action="add_new_photo.php?page=process" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
Choose a photo to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload Photo" />
</form>	
<?php
break;
}
?>

It works like a charm. However, I'd like to follow up on phpSensei's idea, but with a small twist. I want the file to be uploaded but with a different filename. Here is what I tried:

<?php
switch ($_GET['page']) {
case 'process':
	function rand_string($len, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
		$string = '';
		for ($i = 0; $i < $len; $i++) {
			$pos = rand(0, strlen($chars)-1);
			$string .= $chars{$pos};
		}
	return $string; }
	if((!empty($_FILES['uploadedfile'])) && ($_FILES['uploadedfile']['error'] == 0)) {
		$filename = rand_string(10).'.jpg';
		$path = pathinfo($_SERVER['SCRIPT_FILENAME']); $serv_path = str_replace('admin', 'photos', $path['dirname']);
		$ext = substr($filename, strrpos($filename, '.') + 1);
		if (($ext == 'jpg') && ($_FILES['uploadedfile']['type'] == 'image/jpeg') && ($_FILES['uploadedfile']['size'] < 2097152)) {
			$newname = $serv_path.'/'.$filename;
			if (!file_exists($newname)) {
				if ((move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$newname))) {
					echo 'The photo has been saved as: '.$newname;
					$_SESSION['photo_filename'] = $filename;
				} else {
					echo 'Error: A problem occurred during file upload.';
				}
			} else {
				echo 'Error: File '.$_FILES["uploadedfile"]["name"].' already exists.';
			}
		} else {
			echo 'Error: Only .JPG images under 2 megabytes are accepted for upload.';
		}
	} else {
		echo 'Error: No file uploaded.';
	}
break;
default:
?>
<p>Use this form to upload a photo.</p>
<form enctype="multipart/form-data" action="add_new_photo.php?page=process" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
Choose a photo to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload Photo" />
</form>	
<?php
break;
}
?>

However, the name doesn't change when the same image is uploaded. Also, there is a fundamental flaw in naming the file right there, if you know what I mean.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.