php move_uploaded_file unable to move - php

I've got the following code to upload a file and add it to the folder called images located in the root of the server.
$file = $_FILES['prodImg']['tmp_name'];
$newLoc="/images/" . $_FILES['prodImg']['name'];
if(move_uploaded_file($file, $newLoc)){
//do some other code here
}
else{
echo 'error';
}
the form has this button to add the image
<input type="file" name="prodImg" id="prodImg" accept="image/png" />
the images folder has all permissions set to read, write and execute
every time i try to upload an image it go to the else statement.
not sure what i'm doing wrong here.how do i make it work properly?

Do you have attribute in your form?
enctype="multipart/form-data"
I.e.:
<form action="..." method="post" enctype="multipart/form-data">
Then you can try to check permission for that folder.
And also try change filepath:
$newLoc="./images/" . $_FILES['prodImg']['name'];
or
$newLoc="images/" . $_FILES['prodImg']['name'];

The path of your $newLoc variable may be wrong. Try ./images/.
The one specified may define an image folder on the same level as the home, root... folders if you use Linux.

remove the slash ahead of images
$newLoc= "images/".$_FILES['prodImg']['name'];

Related

How to remove image from local directories using php

I am trying to delete images from local folder of my computer using php and form method. My html form is as follows:
<form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Delete Image" name="dlt">
</form>
and my php code is as follows:
if(isset($_POST['dlt'])) {
if(file_exists("F:\xampp\htdocs\practice\images/".$_FILES['file']['name'])) {
$src_file_name = "F:\xampp\htdocs\practice\images/".$_POST['file'];
unlink($src_file_name);
echo '<script>alert("File deleted");</script>';
}
else {
echo"<script>alert('File Doesn't exist);</script>";
}
}
where is the mistake ?
First check if function is called in the same directory where is kept "images" directory because if not - it can not find proper folder. It may be that You are calling PHP function from different location than it is defined and it takes location of the folder in which it is called so it may be path problem.
Also I would recommend to put 'die();' right after unlink() because the website will 'die' and unlink() will echo the error it encounters while trying to unlink the file.
You must ensure that the spliced path is correct, and it is recommended that you use the full path, such as "c:\xxx\images\filename". If the problem cannot be resolved, you are advised to provide error information.

how to get image (file) by $_POST in mvc

i would like to ask, how to get file(image) from my form.php ,then move file in another folder and get path of picture in Controller.php.
I am not sure how to get that file from post.
form.php
<form method="post">
<input name="image" type="file"><br>
<input type="submit" value="Uložit článok" />
</form>
Controller.php
$folder_path "images/" . $_FILES["image"]["name"]
$folder = "images/";
move_uploaded_file($_FILES["image"]["tmp_name"], "$folder" . $_FILES["image"]["name"]);
$Manager->AddPhoto($folder); //this is just adding into database
Add enctype="multipart/form-data" in your form tag. which MVC framework you are using? check the link
It's almost done. The best is decide the final destination by your own.
Let's suppose you have a folder called myPhotos. And the destination file is the datetime or timestamp that you can obtain with microtime(), also you can get the extension of the file with pathinfo. After all the destination files will be:
myphotos/123456myfile.jpg
Having this path, you can use this for move_uploaded_file and later for store in database.

PHP unlink doesn't delete

This tiny problem has ruined my day. I can't delete files by PHP unlink function. I am creating PHP form to update and edit pdf files. Below is my piece of html form and PHP unlink script.
HTML
<form method="post" action="#" enctype="multipart/form-data">
<input type="file" value="<?php echo $row['img']?>" name="image">
<input type="submit" name="update">
</form>
PHP
<?php
if(isset($_POST['update']) && ($_FILES['image']['name'])){
$image=$_POST['image'];
unlink('../pdf/services/'.$image);
}
?>
Try $_FILES. Something like
$image = $_FILES['image']; and then $imgname = $image['name'];
After that you can use unlink(); as you wanted (unlink("../pdf/services".$imgname);)
Hope I helped, It worked after my tests!
Whenever you try to remove a file using unlink, you will need to make sure that everything is ok:
Is the path I want to remove a file from available? Do I have access all the folders I need along the path? If not, then giving the necessary privileges will be needed.
Is my location the one I think it is? You will need to run getcwd() which is current working directory. If your page is not in the desired location, then compared to it the path you want to reach will mean a different thing
Is adding the path I want to the location I got at the second point correct and the one I expect?
If everything above is all right and you still have the problem, then check the error log, it might give you helpful information. If the problem still persists, then you will need to create a dummy file on the path you want to remove the other file from. Is the file created? Is it created at the correct location?

LAMP - file doesn't appear under tmp directory

I am uploading an image using an HTML form.
When I get the full path to the image, it outputs something like this:
'/tmp/phpkIv1BY/10259944_770025219687938_1184503840380306483_n.jpg'
but when I go to the /tmp folder, the sub-folder phpkIv1BY doesn't even exist! What's going on here?
Reason for this behaviour
All uploaded files are temporarily stored in the folder location as defined in
(In php.ini)
upload_tmp_dir =
(Read more about this: http://php.net/upload-tmp-dir)
These temporarily stored files are no longer exist after that php script execution(As mentioned in previous answer request life span) finished.
Do the following to view uploaded file while script executing.
upload.html
<html>
<body>
<form action="upload_file.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>
upload_file.php
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
$fp=fopen("/tmp/write.log","w+");
fputs($fp,"Original Name:".$_FILES["file"]["name"].";temporay Name:".$_FILES["file"]["tmp_name"]."\n");
fclose($fp);
sleep(50000);
}
?>
Upload file by upload.html
In another terminal apply tail in /tmp/write.log after uploading image(Because sleep time is too much so you able to find the image)
#tail -f /tmp/write.log
Copy that temporary file into any place and place the extension of original image file
For example my printed log line is
Original Name:digits.png;temporay Name:/tmp/phpOKlpoK
(Needed to do this with root privilages)
#cp /tmp/phpOKlpoK /home/sandeep/Desktop/file.png
#chown sandeep.sandeep /home/sandeep/Desktop/file.png
So this is the uploaded file that you.
(
Instead of doing all these steps for checking uploaded images you can use move_uploaded_file()
)
I can not find anywhere if PHP might create a temporary folder for temporary uploaded files. But what is certain is that uploaded files are kept temporary and as soon as the request is done, they are removed. So if you think you can find some uploaded file in the /tmp folder, think again.
If you would like some uploaded file to live longer than a request life span, then you need to move it somewhere safe using move_uploaded_file().

Image is Not Getting Saved at Destination Folder

I am a Beginner in PHP and i want to Upload image via PHP and want to save it in destination folder.
i will show you the correct path of the folder where i want the image to be saved, but its only getting saved in database and the image file is not getting save in folder.
The code is as follows:
<?php
if (isset($_POST['submit']))
{
include ('dbconfig.php');
$image=$_FILES['image']['name'];
$name=$_POST['name'];
$detail=$_POST['des'];
if($image!=null)
{
move_uploaded_file($_FILES['image']['tmp_name'],"/Tomar/php/a/uploads/".$_FILES['image']['name']);
}
$entry = "INSERT INTO aboutstudent(stuimg, stuname, studetail) VALUES ('$image','$name','$detail')";
$result = mysql_query($entry);
}
?>
and i want to save my images at below path:
D:/wamp/www/Tomar/php/a/uploads
You can get code example from Image Upload in PHP
make sure you have used enctype="multipart/form-data"
<form action=" " method="post" enctype="multipart/form-data">
and your "uploads" directory has 777 permission.
Referrence : File upload in php

Categories