Hello! I'm having an issue making an image upload form. The form itself shows up and everything of course, just processing it, nothing shows up. The file input form name is 'photo'. Here's the processing code, maybe you guys can help? if(isset($_POST['submit'])){ //Get User Input $username = $_SESSION['username']; $first=$_POST['first']; $last=$_POST['last']; $sex=$_POST['sex']; $description=$_POST['description']; //If image fits the filetype requirements if (($_FILES['photo']['type'] == "image/gif") || ($_FILES['photo']['type'] == "image/jpeg") || ($_FILES['photo']['type'] == "image/jpg") || ($_FILES['photo']['type'] == "image/png")){ $source = $_FILES['photo']['tmp_name']; $target = "profiles/".$FILES_['photo']['name']; copy($source, $target); $image = "profiles/".$_FILES['photo']['name']); $edit="UPDATE members SET first='$first', last='$last', sex='$sex', image='$image', description='$description' WHERE username='$username'"; mysql_query($edit) or die(mysql_error()); echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Redirecting<br><br> <img src="images/loading.gif" border="0"><br><br>Profile Updated. You will be redirected back to the Control Panel in 3 seconds. If you do not wish to wait, <a href="index.php?page=members">Click Here</a>.'; header('Refresh: 3; url=index.php?page=members'); } elseif (($_FILES['photo']['type'] != "image/jpeg") && ($_FILES['photo']['type'] != "image/gif") && ($_FILES['photo']['type'] != "image/jpg") && ($_FILES['photo']['type'] != "image/png")){ //If image does NOT fit the filetype requirements echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Wrong filetype<br><br> <img src="images/loading.gif" border="0"><br><br>Your photo could not be uploaded. Wrong Filetype ('.$_FILES['imagefile']['name'].'). Please make sure your images is a .JPG or .GIF file in order to upload it. You will be redirected back to your Profile in 5 seconds. If you do not wish to wait, <a href="index.php?page=profile">Click Here</a>.'; header('Refresh: 5; url=index.php?page=profile'); } elseif (!isset($_FILES['photo'])) { //If user chose not to upload an image $edit="UPDATE members SET first='$first', last='$last', sex='$sex', description='$description' WHERE username='$username'"; mysql_query($edit) or die(mysql_error()); echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Redirecting<br><br> <img src="images/loading.gif" border="0"><br><br>Profile Updated. You will be redirected back to the Control Panel in 3 seconds. If you do not wish to wait, <a href="index.php?page=members">Click Here</a>.'; header('Refresh: 3; url=index.php?page=members'); } } elseif(!isset($_POST['submit'])) { header("Location: index.php?page=profile"); } PHP: The directory has all the write permissions allowed so it can't be that. I'm wondering if I'm missing something, or if my host simply doesn't allow for PHP uploading. The processing code worked up until I added the if() statements about the image filetype and upload, etc.
hi, As an addition, check also or validate the file size of the image coz maybe you php directive is set to as low as 2mb and your uploading a image more than that file size. Another thing is check your html form tags...it should have <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">--> the enctype part. and also check line 12 $target = "profiles/".$FILES_['photo']['name']; notice the $FILES... i suggest you set the error so it will display the error to you. I hope this help. Thanks, gigamike
In additon to gigamike, i would suggest looking at or using the following php function http://www.php.net/manual/en/function.move-uploaded-file.php Also may be worth checking your form for mis types, form submit name is "submit" not "Submit" and your file name in the form is set to photo
Update: I got the upload portion to work with validation of proper filetypes for images (png/jpg/ etc), but what if I want to make it so that if users choose not to upload a file, it will still process the rest of the data? Is there an option to see if a "file" type field is set? Like, isset($_FILES['photo']) ? I've tried that code, but it doesn't seem to work. It acts as if the file is a wrong filetype. Any ideas? Here's the new code I've got that works for uploading a specified image, but doesn't work if the user chooses not to upload an image (blank "file" field): if(isset($_POST['submit'])){ //Get User Input $username = $_SESSION['username']; $first=$_POST['first']; $last=$_POST['last']; $sex=$_POST['sex']; $description=$_POST['description']; $target_path = "profiles/"; $target_path = $target_path . basename( $_FILES['photo']['name']); $_FILES['photo']['tmp_name']; //If image fits the filetype requirements if (isset($_FILES['photo'])){ if (($_FILES['photo']['type'] == "image/gif") || ($_FILES['photo']['type'] == "image/jpeg") || ($_FILES['photo']['type'] == "image/jpg") || ($_FILES['photo']['type'] == "image/png")){ if(move_uploaded_file($_FILES['photo']['tmp_name'], $target_path)) { $edit="UPDATE members SET first='$first', last='$last', sex='$sex', image='$target_path', description='$description' WHERE username='$username'"; mysql_query($edit) or die(mysql_error()); echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Redirecting<br><br> <img src="images/loading.gif" border="0"><br><br>Profile Updated. You will be redirected back to the Control Panel in 3 seconds. If you do not wish to wait, <a href="index.php?page=members">Click Here</a>.'; header('Refresh: 3; url=index.php?page=members'); } else { echo '<center>Image Upload Error!</center>'; } } elseif (($_FILES['photo']['type'] != "image/jpeg") && ($_FILES['photo']['type'] != "image/gif") && ($_FILES['photo']['type'] != "image/jpg") && ($_FILES['photo']['type'] != "image/png")){ //If image does NOT fit the filetype requirements echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Wrong filetype<br><br> <img src="images/loading.gif" border="0"><br><br>Your photo could not be uploaded. Wrong Filetype ('.$_FILES['photo']['name'].'). Please make sure your images is a .JPG or .GIF file in order to upload it. You will be redirected back to your Profile in 5 seconds. If you do not wish to wait, <a href="index.php?page=profile">Click Here</a>.'; header('Refresh: 5; url=index.php?page=profile'); } } elseif (!isset($_FILES['photo'])) { //If user chose not to upload an image $edit1="UPDATE members SET first='$first', last='$last', sex='$sex', description='$description' WHERE username='$username'"; mysql_query($edit1) or die(mysql_error()); echo '<center><br><br><Br><img src="images/profile_header.jpg" border="0" alt="Profile"><br><br>Redirecting<br><br> <img src="images/loading.gif" border="0"><br><br>Profile Updated. You will be redirected back to the Control Panel in 3 seconds. If you do not wish to wait, <a href="index.php?page=members">Click Here</a>.'; header('Refresh: 3; url=index.php?page=members'); } PHP:
You are my savior Your code worked like a charm after I changed it to $_FILES instead Thank you so much!