file_put_contents not working on PHP versions 5.4 and higher

683 views Asked by At

The following code does not work on PHP version 5.4 and higher on my server due to deprecation. Changing PHP version to 5.3 or lower with MultiPHP Manager, however, works fine.

What should I do to the following code to make it work for updated versions?

    <?php
//blog.theonlytutorials.com
//author: agurchand

if($_POST){
//get the url
$url = $_POST['url'];

//add time to the current filename
$name = basename($url);
list($txt, $ext) = explode(".", $name);
$name = $txt.time();
$name = $name.".".$ext;

//here is the actual code to get the file from the url and save it to the uploads folder
//get the file from the url using file_get_contents and put it into the folder using file_put_contents
$upload = file_put_contents("uploads/$name",file_get_contents($url));
//check success
if($upload)  echo "Success: <a href='uploads/".$name."' target='_blank'>Check Uploaded</a>"; else "please check your folder permission";
}
?>

<html>
<head><title>Theonlytutorials - Simple File Upload from URL Script!</title></head>
<body>
<h3>Theonlytutorials.com - Very Simple File Upload from URL Script!</h3>
    <form action="" method="post">
        Your URL: <input type="text" name="url" />
    </form>
</body>

</html>
1

There are 1 answers

0
Robert On

If you put data with file_put_contents() on one server (or xampp) and then retrieve the info with file_get_contents() on another server, the function may not work. I ran the code to put the data into the file and then read it back with file_get_contents(), and it worked. In other words, if it doesn't seem to be working, make sure you are reading from a file that you created on the same server.

Other than that, see other ideas about having correct settings in php.ini