By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 709 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

Copying remote files using fopen and fwrite

P: 60
i wanted a remote file uploader in php, i mean a form where you send the URI of a file on other web and the script saves it locally.

i tried:

[PHP]<? if(empty($_POST)){ ?>
<html>
<body>
<form method=POST>
<b>URL del Archivo </b>
<input type=text name=uri><br>
<input type=text name=srv>
<input type=submit value=Descargar>
</form>
</body>
</html>
<?
}else{
$file = $_POST['uri'];

$ges = fopen($file, "r");
$ges = fwrite($ges, "\.\/some.thing");
}
?>[/PHP]

1. I know there's something wrong but don't know what

2. How can i store it with the original name and extension ?


thanks
May 22 '07 #1
Share this Question
Share on Google+
3 Replies


pbmods
Expert 5K+
P: 5,821
Changed thread title to better match contents.

Expand|Select|Wrap|Line Numbers
  1. $ges = fopen($file, "r");
  2. $ges = fwrite($ges, "\.\/some.thing");
  3. }
  4. ?>
1. I know there's something wrong but don't know what

2. How can i store it with the original name and extension ?
1. You're opening a file with read access ('r'), then you try to write to it. Nope. :P

http://php.net/fopen

2. You want to do something like this:
Expand|Select|Wrap|Line Numbers
  1. $file = basename($_POST['uri']);
  2. copy("/path/to/source/$file", "/path/to/dest/$file");
  3.  
http://php.net/copy
May 23 '07 #2

P: 60
[PHP]<? if(empty($_POST)){ ?>
<html>
<body>
<form method=POST>
<b>URL del Archivo </b>
<input type=text name=uri><br>
<center><input type=submit value=Descargar></center>
</form>
</body>
</html>
<?
}else{
$uri = $_POST['uri'];
$file = basename($_POST['uri']);
copy("$uri", "$file");
echo $file."<br>".$uri;
}
?>[/PHP]

ok, right ??
May 23 '07 #3

pbmods
Expert 5K+
P: 5,821
ok, right ??
Looks pretty good to me. Note that you will be copying the file to the same directory as your script.

If this doesn't work (I can't remember if copy allows you to use URLs or not), try using file_put_contents instead.
May 23 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.