My php program is not moving uploaded image to a directory (ubuntu)
I am a beginning in Php.
I have been trying for several hours to move an uploaded image to a directory without success. I have already read other questions about the subject, but I have not been able to solve the problem:
1) I have already checked if the directory exists (see output below);
2) It seems that the directory is not writable, but I am not only trying to save the image in a kind of system directory, but also in a simple directory of my computer.
3) The image seems to be uploaded correctly.
4) I have already used "sudo chmod -R 775 /home/daniel/NetBeansProjects/NewPhpProject/photos", but this should not be the problem, since this is not a system directory!
I have no idea what is happening.
Daniel
See my code below:
uploadImage.php
<html>
<body>
<h2> Upload your photo. </h2>
<form action="moveImage.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
movingImage.php
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
//$upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/photos/";
//$upload_dir = dirname(__FILE__) . "/photos/";
$upload_dir ="/home/daniel/NetBeansProjects/NewPhpProject". "/photos/";
echo "upload_dir: " . $upload_dir . "<br>";
if (file_exists($upload_dir)) {
if (is_writable($upload_dir)) {
$target = $upload_dir; //"dirname(__FILE__)" . "photos/";
$target = $target . basename($_FILES['file']['name']);
$moved = move_uploaded_file($_FILES['file']['name'], "$target");
} else {
echo 'Upload directory is not writable<br>';
}
} else {
echo 'Upload directory does not exist.<br>';
}
echo $target . "<br>";
// echo dirname(__FILE__)."<br>";
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
Output:
upload_dir: /home/daniel/NetBeansProjects/NewPhpProject/photos/
Upload directory is not writable
Upload: FotoCaju.jpeg
Type: image/jpeg
Size: 0.666015625 kB
Stored in: /tmp/phpmTJWMi
The problem is probably that every directory along the way to /home/daniel/NetBeansProjects/NewPhpProject/photos/
must also be writable by Apache.
Since changing permissions to your home directory is generally not a good idea, you should make a symbolic link somewhere that Apache has access to that is outside of public_html
(like /var/www
) that points to your target directory, which should have the correct permissions for the Apache user.
You need to change the directory permissions for /home/daniel/NetBeansProjects/NewPhpProject/photos/
. For this, you will use the chmod command.
$: chmod +w /home/daniel/NetBeansProjects/NewPhpProject/photos/
PHP.net site comment is_writable:
Note: The results of this function are cached. See clearstatcache() for more details.
Try it. Maybe the result is cached with the result before you have changed the permissions of the folder.
链接地址: http://www.djcxy.com/p/78076.html