[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How to copy a file in PHP using the URL

Posted on 2003-03-09
4
Medium Priority
?
230 Views
Last Modified: 2009-12-16
Hello

I'm trying to copy a file using the whole URL (from one URL to my server). I've tried with fopen, but that doesn't seem to work. Any clever ideas on how to do this? Anybody got an example lying around?
0
Comment
Question by:njaha
4 Comments
 
LVL 15

Accepted Solution

by:
VGR earned 1200 total points
ID: 8101301
yes, of course.

For text files, what you tried should work.
For binary files (like images) :

// $file : the URI of the resource (file) to access and copy
// $file : the local name relative to the current path
// $checkexist : explicit ; no copy if already exists if TRUE, overwrite if FALSE
//
function CopieImage($file,$nfile,$checkexist=TRUE) {
  GLOBAL $globNbImgManqu;
  $res=TRUE;
  if ($checkexist) {
    $go=(! file_exists($nfile));
  } else $go=TRUE;
  if ($go) {
    $res=FALSE;
    $fp = @fopen($file,"rb");
    if ($fp) { // car False si pas bon
      $cont='';
      $res=TRUE;
      set_magic_quotes_runtime(0);
      while(!feof($fp)) $cont.= fread($fp,8192);
      fclose($fp);
      $fp2 = @fopen($nfile,"wb");
      fwrite($fp2,$cont);
      fclose($fp2);
      //set_magic_quotes_runtime(get_magic_quotes_gpc());
    } // else RàF, reste Faux
  } // else RàF, reste Vrai
  if (! $res) $globNbImgManqu++; // faux : échec
  return($res);
} // CopieImage Boolean Function
0
 

Author Comment

by:njaha
ID: 8103128
Thanks a lot!
0
 

Expert Comment

by:Simokas
ID: 8109106
Liitle script that I use. This allows to copy image with new name.

function do_upload($filename,$newname) {
  $file = basename($filename);
  $tmp_upload_path = "$path_to_upload";
  $new_file_name = $path_to_upload.".$newname;
  if (!copy($tmp_upload_path.$file, $new_file_name)) echo "failed to copy file\n";
  return;
}
0
 
LVL 1

Expert Comment

by:Hobbiett
ID: 8129014
Are you happy with the solution of VGR?

Then Accept his Comment and give him the points
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question