如何在file_put_contents中重命名文件? [关闭]

I have this code snippet that grabs a SWF file and saves it locally. Works like a charm! However, I need to rename the file and have no idea how to do so. All help is greatly appreciated! Thanks!

$filename = "filename"; //Without extension
$urls = array($swf_url);
foreach($urls as $url) {
   $data = file_get_contents($url);
   file_put_contents('/home/public/'.basename($url), $data);
}

... Just give it a different name in the first place...

file_put_contents('/home/public/' . $someothername, $data);

Simply save it with the name you want:

file_put_contents('/home/public/whatever/you/want.swf', $data);
rename('oldname.swf', 'newname.swf');

You can read more in the documentation.

Why do you not simply change where you're writing it in the first place?

If you read the documentation of file_put_contents, you'll know that the first parameter $filename is the filename. All you need to do to write it to a different place is change that first parameter.

Namely, modify '/home/public/'.basename($url) to be whatever you want.

If you really want to rename a file, you can use the rename method of PHP like so:

rename('/home/public/' . basename($url), '/home/public/newname.swf');

.. but that makes little sense.