?
Solved

How to copy a file in PHP using the URL

Posted on 2003-03-09
4
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

765 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