Reputation: 1
I have a form in which I can upload an image. However for some reason I am not able to upload the image. I have updated the file permissions (777 permissions) and still the file does not upload. The form seems to be working because the text (not included in the form is updating)
<form action="manage_content.php?project=5" method="post" enctype="multipart/form-data">
<div class="width">
<p>Project Image: <br>
<input type="file" name="image_file" id="file" class="inputfile" />
<label for="file">Choose a file</label>
</p>
</div>
<input type="submit" name="update_project" value="Update Current Project">
</form>
The PHP form processing.
if (isset($_POST['update_project'])) {
$required_fields = array("project_name", "date", "content");
validate_presences($required_fields);
$fields_with_max_length = array("project_name" => 30);
validate_max_length($fields_with_max_length);
$id = $current_project["id"];
$project_name = mysql_prep($_POST["project_name"]);
$date = mysql_prep($_POST["date"]);
$content = mysql_prep($_POST["content"]);
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["image_file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
print_r($_FILES);
echo "<br>" . $target_file;
if (empty($errors)) {
if($_FILES["image_file"]["error"] == 4) {
//means there is no file uploaded
$query = "";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) >= 0) {
$_SESSION["message"] = "Subject Updated";
// redirect_to("manage_content.php?subject=$current_subject[id]");
}
} else {
$query = "";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) >= 0) {
$_SESSION["message"] = "Subject Updated";
// redirect_to("manage_content.php?subject=$current_subject[id]");
}
}
} else {
$message = "Subject creation failed.";
}
} else {
// probably a get request
// end of post submit
}
?>
print_r($_FILES) gives me:
Array ( [image_file] => Array (
[name] => 1.jpg
[type] => image/jpeg
[tmp_name] => C:\xampp\tmp\phpF5AA.tmp
[error] => 0
[size] => 24397 )
)
and echo $target_file gives: images/1.jpg
Upvotes: 0
Views: 58
Reputation: 21
Be sure that the directory into which you are uploading has the same permissions and ownership as the program uploading the file. It should have write permissions for the user. The same will go for the file if it already exists, you must have write permissions in order to replace the file.
Upvotes: 2