can't find error in file copy script

images php coding issues or problems here.

Moderators: macek, egami, gesf

vladimirg
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Nov 18, 2010 5:15 am

can't find error in file copy script

Postby vladimirg » Thu Nov 18, 2010 5:29 am

here is a very simple script, which should copy image file:

Code: Select all

<?php
echo "<p><img src=\"http://www.leonardo.sk/obrazky/6267.jpg\"></p>";
if (copy("http://www.leonardo.sk/obrazky/6267.jpg","temp.jpg")) echo "<p>Copied.</p>";
else echo "<p>Error.</p>";
?>

the image is normally accesive (you can see it), but copy function gives error: "Unable to access http://www.leonardo.sk/obrazky/6267.jpg"
i don't understand why.
can anybody help please?

lokanath7
New php-forum User
New php-forum User
Posts: 139
Joined: Mon Nov 15, 2010 12:11 am

Re: can't find error in file copy script

Postby lokanath7 » Fri Nov 19, 2010 7:52 am

ur code is fine
the copy() will work on both php 4 nd 5
which version u r using ?

vladimirg
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Nov 18, 2010 5:15 am

Re: can't find error in file copy script

Postby vladimirg » Fri Nov 19, 2010 9:49 am

i m using php5. thank you for reply. i have solved this problem meanwhile with a small trick:

Code: Select all

<?php
      $in = file_get_contents('http://www.leonardo.sk/obrazky/6267.jpg');
      $file = '6267.jpg';
      if (file_put_contents($file, $in)) echo "Copy hotovo.<br><img src=\"6267.jpg\">";
      else echo "Chyba<br>";
?>

so thank s all.

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: can't find error in file copy script

Postby egami » Fri Nov 19, 2010 10:06 am

take a look at php.net/basename

vladimirg
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Nov 18, 2010 5:15 am

Re: can't find error in file copy script

Postby vladimirg » Fri Nov 19, 2010 10:15 am

egami wrote:take a look at php.net/basename

i don t think basename has something with this.

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: can't find error in file copy script

Postby egami » Fri Nov 19, 2010 10:29 am

Code: Select all


$URL = 'http://www.leonardo.sk/obrazky/6267.jpg';
$filename = basename($URL);

if (!@copy($URL,$filename))
{
        $errors = error_get_last();
        echo "COPY ERROR: ".$errors['type'];
        echo "<br />\n".$errors['message'];
} else {
        echo "File Copied!";
}

vladimirg
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Nov 18, 2010 5:15 am

Re: can't find error in file copy script

Postby vladimirg » Fri Nov 19, 2010 10:52 am

egami wrote:

Code: Select all


$URL = 'http://www.leonardo.sk/obrazky/6267.jpg';
$filename = basename($URL);

if (!@copy($URL,$filename))
{
        $errors = error_get_last();
        echo "COPY ERROR: ".$errors['type'];
        echo "<br />\n".$errors['message'];
} else {
        echo "File Copied!";
}

:-) sorry. i have forgotten to write. i am running the script on my local machine.
your script only gets the basename of the URL. this wasn t my problem. i have founded out that my problem was unsufficient user rights for file copying. my second version of script has worked this around.

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: can't find error in file copy script

Postby egami » Mon Nov 22, 2010 11:21 am

Yes, but my script makes for more flexibility.
For example, I can feed any URL into it, and filename becomes the picture that I need, and not statically mapped.

Also, makes the rest of the code less confusing, less to write, less to troublehsoot.

anyway, it was merely a suggestion, you don't have to do anything except breathe. :)

-B

phpmike
New php-forum User
New php-forum User
Posts: 37
Joined: Thu Nov 04, 2010 3:16 pm

Re: can't find error in file copy script

Postby phpmike » Fri Nov 26, 2010 3:57 pm

The reason might be the permissions on the directory as well.

mottihoresh
New php-forum User
New php-forum User
Posts: 2
Joined: Sat Feb 19, 2011 7:36 pm

Re: can't find error in file copy script

Postby mottihoresh » Sat Feb 19, 2011 8:08 pm

I think the only problem you had is that you didn't have the right permission to write to that directory.


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest

cron