Link to home
Start Free TrialLog in
Avatar of Adebayo Ojo
Adebayo OjoFlag for Nigeria

asked on

Issue with copying file between sub directories in PHP

I have a PHP script, signup.php inside this directory: /var/www/html/signup.php
Inside this signup.php file, I have the below lines of code to copy file between sub-directories:
$avatar = "images/avatardefault.png";
        $avatar2 = "var/www/html/user/avatardefault.png";
        if (!copy($avatar, $avatar2)) {
            echo "failed to create avatar.";
        }

Open in new window

But when I run this script, I get an error:
PHP Warning:  copy(var/www/html/user/avatardefault.png): failed to open stream: No such file or directory in /var/www/html/signup.php

Open in new window


Please what could be my mistake?
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

I am guessing the following folder does not exist
var/www/html/user

Open in new window

Check in the var/www/html folder to see if there is
a) A folder called user
b) It is all lower case (on Unix systems User != user
This line:

$avatar2 = "var/www/html/user/avatardefault.png";

...probably needs an initial slash:

$avatar2 = "/var/www/html/user/avatardefault.png";

Otherwise, it is taking your CURRENT folder, /var/www/html/, and then looking for that path INSIDE of it, like this:

/var/www/html/var/www/html/user/avatardefault.png
@Adebayo,

Do you still require assistance with this question? If so post back here, otherwise please can you close the question.
Avatar of Adebayo Ojo

ASKER

This issue has to do with the owner and permission of that file "signup.php"
I was able to copy once the owner and permission was corrected.
Thanks. Yes you can close the question.
That doesn't make sense. The error message has nothing to do with permissions / ownership. If it were a permissions issues, you'd get an error related to permissions being denied (and the code would not have been able to be executed), and the code you showed definitely had the relative path issue. Are you certain you didn't have multiple problems that all contributed to the issue?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.