从网址上传图片

时间:2014-03-01 23:47:23

标签: php url file-upload upload

我有一个现有的脚本,我正在尝试修改而不是从PC上选择要上传的文件,而是添加相关图像的URL。我一直在尝试使用get file函数,我可以用它来从url中获取文件并上传到服务器,但是我想让它捕获文件并传递给现有脚本的其余部分以完成调整大小等

见下文......

    $thumburl = "http://url of image file"; 
    $thumb = file_get_contents($thumburl);

    $space  = $_POST['thumb']['size'];
    move_uploaded_file($_POST['thumb']['tmp_name'], $config['TMP_DIR']. '/thumbs/' .$thumb);
   @chmod($config['TMP_DIR']. '/' .$thumb, 0644);
        $ff = $config['TMP_DIR']. '/thumbs/' .$thumb;
        $fd = $config['TMB_DIR']. '/' .$thid. '.jpg';
        createThumb($ff, $fd, $config['img_max_width'], $config['img_max_height']);
        copy($fd, $config['TMB_DIR']. '/1_' .$thid. '.jpg');
        copy($fd, $config['TMB_DIR']. '/2_' .$thid. '.jpg');
        copy($fd, $config['TMB_DIR']. '/3_' .$thid. '.jpg');
        @unlink($config['TMP_DIR']. '/thumbs/' .$thumb);

1 个答案:

答案 0 :(得分:0)

变化

$thumburl = "http://url of image file"; 
$thumb = file_get_contents($thumburl);

$space  = $_POST['thumb']['size'];
move_uploaded_file($_POST['thumb']['tmp_name'], $config['TMP_DIR']. '/thumbs/' .$thumb);
@chmod($config['TMP_DIR']. '/' .$thumb, 0644);

为:

$thumburl = "http://url of image file"; 
$thumbContent = file_get_contents($thumburl);

$thumb = tempnam($config['TMP_DIR'] . '/thumbs/', 'FOO');
file_put_contents($config['TMP_DIR'] . '/thumbs/' . $thumb, $thumbContent);
@chmod($config['TMP_DIR'] . '/thumbs/' . $thumb, 0644);