1

I have a little PHP code to upload my files, all the files are pictures .

I want to make some changes in it.

HTML codes :

<form action="" enctype="multipart/form-data" method="post">
<input id="file" name="file" type="file" />
<input id="Submit" name="submit" type="submit" value="Submit" />
</form>

PHP code :

<?php

// Upload and Rename File

if (isset($_POST['submit']))
{
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$filesize = $_FILES["file"]["size"];
$allowed_file_types = array('.doc','.docx','.rtf','.pdf');

if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
{
// Rename file
$newfilename = md5($file_basename) . $file_ext;
if (file_exists("upload/" . $newfilename))
{
// file already exists error
echo "You have already uploaded this file.";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename);
echo "File uploaded successfully.";
}
}
elseif (empty($file_basename))
{
// file selection error
echo "Please select a file to upload.";
}
elseif ($filesize > 200000)
{
// file size error
echo "The file you are trying to upload is too large.";
}
else
{
// file type error
echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types);
unlink($_FILES["file"]["tmp_name"]);
}
}

?>

My question is that i want to upload files , from address .

How should the php code change ?

Thanks.

  • You need to be more specific. "from address" makes no sense. Could you please try to restructure your question? What address? What does it do now, compared to what you want it to do? – h2ooooooo Feb 03 '14 at 15:19
  • file from address means file from web address or local file explorer address? –  Feb 03 '14 at 15:19
  • In that case, there's no uploading involved. You need to use cURL. See: http://stackoverflow.com/questions/6476212/save-image-from-url-with-curl-php –  Feb 03 '14 at 15:21
  • if you mean web address, pasting it to file selector automatically downloads and adds it. – Volkan Ulukut Feb 03 '14 at 15:21
  • @h2ooooooo address is from web address – user3248595 Feb 03 '14 at 15:21
  • if its from the web address you can try file_get_contents("web_address")..read about it here http://ca1.php.net/file_get_contents – Works On Mine Feb 03 '14 at 15:22

2 Answers2

2

If you mean "download the contents of an URL to a local file", you can do so by using fopen, feof, fwrite, fread and fclose:

<?php
    function downloadFile($fromURL, $toPath, $bufferSize = 1024) {
        $loadHandle = fopen($fromURL, 'rb');

        if ($loadHandle !== false) {
            $saveHandle = fopen($toPath, 'ab');

            if ($saveHandle !== false) {
                while (!feof($loadHandle)) {
                    fwrite($saveHandle, fread($loadHandle, $bufferSize));
                }


                fclose($loadHandle);
                fclose($saveHandle);

                return true;
            } else {
                fclose($loadHandle);

                throw new Exception('Could not open local file');
            }
        } else {
            throw new Exception('Could not load URL');
        }

        return false;
    }
?>

Usage:

<?php   
    try {
        downloadFile(
            'https://www.gravatar.com/avatar/8bf82b417151bafc01248013d984f53f?s=32&d=identicon&r=PG&f=1', 
            '/var/www/download/avatar.png'
        );
    } catch (Exception $e) {
        echo 'Could not download file: ' . $e->getMessage();
    }
?>
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
0

This is not called 'file upload', it's called downloading file from remote server and saving it locally. Check several solutions below:

Need php script to download a file on a remote server and save locally

Community
  • 1
  • 1
Val Petruchek
  • 176
  • 1
  • 1
  • 9