Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a form that allows you to upload an image (this is not mandatory). If the user does not upload an image I want it to set to a default image (default.jpg).

This is what I have:

PHP
$uploadDir = 'images/'; //Image Upload Folder
			if (isset($_POST['submit'])){
				$fileName = $_FILES['Photo']['name'];
				$tmpName  = $_FILES['Photo']['tmp_name'];
				$fileSize = $_FILES['Photo']['size'];
				$fileType = $_FILES['Photo']['type'];
				$filePath = $uploadDir . $fileName;
				$result = move_uploaded_file($tmpName, $filePath);

				if (!$result) {
					echo' An error occurred';
					exit();
				}
				if(!get_magic_quotes_gpc())
				{
					//addslashes adds a backslash before any malicious characters, for security
					$fileName = addslashes($fileName);
					$filePath = addslashes($filePath);
				};


The code above works fine with uploading an image, I just dont know how or where I can set it to default if none is provided. I have tried isset and empty but I keep getting the error from the if(!result) part.
Posted

1 solution

Can't you just replace the echo 'An error occured'; part with something providing the default image?
 
Share this answer
 
Comments
jba1991 2-Apr-15 14:34pm    
Yeah I can and I've just done that. Thanks, I think I need a break!
phil.o 2-Apr-15 14:35pm    
You're welcome :)
Sergey Alexandrovich Kryukov 2-Apr-15 14:38pm    
Of course. A 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900