I am trying to copy an image from one folder to another. I feel that my paths are wrong, but I have tried so many path combinations that I am now unsure whether it is the problem.
I am trying to copy an image (if it exists) from the user_photos
folder, into the profile_pics
folder. Here are where they are located, as well as the script, known as, change_dp.php
, which once called, will execute the copy()
.
www > user_data > user_photos > photos_are_here
www > user_data > profile_pics > photos_are_here
www > inc > change_dp.php
Here is change_dp.php
:
$all_pics = mysqli_query ($connect, "SELECT * FROM user_photos WHERE username = '$username'");
$row_query = mysqli_fetch_assoc($all_pics);
$get_photo_owner = $row_query['username'];
$get_photo_id = $_GET['id'];
$get_pic_with_id = mysqli_query ($connect, "SELECT * FROM user_photos WHERE id = '$get_photo_id'");
$row_query2 = mysqli_fetch_assoc($get_pic_with_id);
$img_url = $row_query2['img_url'];
$file = $img_url;
$arrPathInfo = pathinfo($file);
$shortened_url = $arrPathInfo['basename'];
if (file_exists($file)) {
copy("../../" . $file, "../../profile_pics/" . $shortened_url);
}
$pro_pic_change = mysqli_query($connect, "UPDATE users SET profile_pic='$shortened_url' WHERE username = '$username'") or die ($connect);
header("Location: /photos/$get_photo_owner");
Just to illustrate the issue more:
alice.jpg
, it will be stored as user_data/user_photos/alice.jpg
. This is why I have varible $shortened_url
which will only get the base name of the file ie get the alice.jpg
from user_data/user_photos/alice.jpg
. I need the $shortened_url
because column profile_pics
in the database holds just the base name of the image, not the full path. if statement
, I am trying to get the basename of the image, see if an image with the same name in my user_photos
file exists, and if it does, copy it into the profile_pics
folder and set the basename to profile_pics
column via an UPDATE statement. The UPDATE statement works fine, the image name does change in the database, but the image just doesn't copy over, which corrupts the image since it cant find it in the profile_pics
folder. What I have tried:
copy($file, "../profile_pics/" . $shortened_url);
copy("../../" . $file, "../../profile_pics/" . $shortened_url);
copy($file, "user_data/profile_pics/" . $shortened_url);
None which work.
Edit :
Here's what I want the code to do:
Lets assign values to $file
for an example:
$file = user_data/user_photos/alice.jpg
user_photos
folder. alice.jpg
in the folder, then copy that image into the profile_pics
folder. You could use __DIR__
magic constant and then based on that you can go to upper levels in the directory hierarchy, by using realpath()
function or even dirname()
function. This way it will be less confusing and less error prone.
Eg:
$upOneLevel = realpath(__DIR__."../");
$upTwoLevels = realpath(__DIR__."../../");
So to access your file you shoud use:
$fileRealPath = $upOneLevel."/".$file;
$fileRealDestination = $upOneLevel."user_data/profile_pics/".$shortened_url;
I also recommend to update the data in the DB only if the file exists and the copy command executes successfully:
if (file_exists($fileRealPath)) {
if(copy($fileRealPath, $fileRealDestination)) {
//TODO db logic here
} else {
//TODO error handling here
}
} else {
//TODO error handling here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.