1

Trying to build a form where for uploading images via url and have it copied from there to my server.

Searching the follwoing threads:

I've tried this:

$pic = Submitted image URL
$pic = Submitted image name . ".jpg"

copy($pic, $_SERVER['DOCUMENT_ROOT'] . '/mydir/' . $picname);

Results in:

Warning: copy(mydir/mypic.jpg) [function.copy]: failed to open stream: No such file or directory in /home/user/public_html/mysite.ca/upload.php on line 42

This:

$fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/mydir/' . $picname, "w");
fwrite($fp, $pic);
fclose($fp);

Results in:

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/user/public_html/mysite.ca/upload.php on line 47

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/user/public_html/mysite.ca/upload.php on line 48

Directory permissions are 777, their is no personal info anywhere on the server. There is no error in any of the logs, I'm not sure how to troubleshoot this?

Community
  • 1
  • 1
HandsomeRob
  • 445
  • 3
  • 7
  • 14

3 Answers3

1

Directly opening URLs like files only works if your server has the allow_url_fopen option enabled.

fopen() is failing and returning FALSE but you are not checking that. There is something wrong with your filename or directory permissions.

NovaDenizen
  • 5,089
  • 14
  • 28
  • If working on localhost would this be in php.ini? Can't find any reference to it on cPanel on the web, something for admin to do? – HandsomeRob May 04 '13 at 17:57
  • `allow_url_fopen=off` would generate a different error message when using `copy` though … – CBroe May 04 '13 at 18:29
0

$fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/mydir/' . $picname, "w"); this seems problem. do this in two parts like, $path = $_SERVER['DOCUMENT_ROOT'] . '/mydir/'.$picname; $fp = fopen($path, "w"); . now you can echo $path and see whether it's correct? if still problem, try using relative path, like, $path = "./mydir/$picname"; echo $path; it and directly go to that folder to check whether folder exists.

web2students.com
  • 307
  • 2
  • 16
0

Warning: copy(mydir/mypic.jpg) [function.copy]: failed to open stream: No such file or directory in /home/user/public_html/mysite.ca/upload.php on line 42

That sounds like the destination path you’re trying to copy the file to is wrong.

Have you checked what the path actually contains?

var_dump($_SERVER['DOCUMENT_ROOT'] . '/mydir/' . $picname);

Apart from the current error, you are ripping a huge security hole in your server if you accept any path/file name information the client sends to build the name to save the image under without any further control/restrictions.

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • Directory name is a defined constant outside the public directory, just easier to describe this way. This form is only for my use to upload a large quantity of pictures. – HandsomeRob May 04 '13 at 17:41
  • var_dump() string(79) "/home/user/public_html/mysite.ca/mydir/mypic.jpg" – HandsomeRob May 04 '13 at 17:51
  • And the folder `mydir` in the same folder where you are running your script in already exists …? If not, you have to create it (`mkdir`) before you can copy a file into it! – CBroe May 04 '13 at 18:30