php - 使用 PHP 通过 url 上传 1000 张图像

标签 php download uploading

我想通过 URL 一键上传 1000 张图片。我在 MYSQL 数据库中存储了 1000 个图像 URL。

所以请任何人给我 PHP 代码,通过 URL 通过 mysql 数据库上传那 1000 张图像。

目前我正在使用以下代码:- 它通过发布图像的 URL 每次点击上传一张图像...

但我想通过从数据库获取 URL 来一键上传 1000 张图片

$result = mysql_query("SELECT * FROM thumb") or die(mysql_error());

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {

echo "<div>";
$oid = $row['tid'];
$th= $row['q'];

echo "</div>";

$thi = $th;
$get_url = $post["url"];
$url = trim('$get_url');

if($url){
    $file = fopen($url,"rb");
    $directory = "thumbnail/";
    $valid_exts = array("php","jpeg","gif","png","doc","docx","jpg","html","asp","xml","JPEG","bmp"); 
    $ext = end(explode(".",strtolower(basename($url))));
    if(in_array($ext,$valid_exts)){

        $filename = "$oid.$ext";
        $newfile = fopen($directory . $filename, "wb");
        if($newfile){
            while(!feof($file)){
                fwrite($newfile,fread($file,1024 * 8),1024 * 8);
            }
            echo 'File uploaded successfully';
            echo '**$$**'.$filename;
        }
        else{
            echo 'File does not exists';
        }
    }
    else{
        echo 'Invalid URL';
    }
}
else{
    echo 'Please enter the URL';
}

}

非常感谢...... ...

最佳答案

您拥有的代码已经过时,并且比需要的复杂得多。这不是一个您可以因为询问而获得代码的网站,这是一个学习环境。

我会给你一个例子,你可以继续:

// Select the images (those we haven't done yet):
$sItems = mysql_query("SELECT id,url FROM thumb WHERE imported=0") or die(mysql_error());
// Loop through them:
while( $fItems = mysql_fetch_assoc($sItems) ){
    $imgSource = file_get_contents($fItems['url']); // get the image
    // Check if it didn't go wrong:
    if( $imgSource!==false ){
        // Which directory to put the file in:
        $newLocation = $_SERVER['DOCUMENT_ROOT']."/Location/to/dir/"; 
        // The name of the file:
        $newFilename = basename($fItems['url'], $imgSource); 

        // Save on your server:
        file_put_content($newLocation.$newFilename);
    }
    // Update the row in the DB. If something goes wrong, you don't have to do all of them again:
    mysql_query("UPDATE thumb SET imported=1 WHERE id=".$fItems['id']." LIMIT 1") or die(mysql_error());
}

相关功能:
file_get_contents() - 获取图片内容
file_put_contents() - 将此函数中给出的内容放入指定的文件中
basename() - 给定一个网址,它只给你文件名

重要:

  • 您正在使用 mysql_query。这已被弃用(不应再使用),请使用 PDO 或 mysqli 代替
  • 我建议您通过命令行完成此工作,并在更新后添加回显,以便您可以监控进度

关于php - 使用 PHP 通过 url 上传 1000 张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33917078/

相关文章:

http - 在后台下载大文件,从浏览器启动

WCF 分块/流媒体

android - 如何向 Google Play 支付上传应用程序的费用

php - MySQL 状态模型——最佳实现?

php - 如何解决 SyntaxError : JSON. parse: unexpected character at line 1 column 1 of the JSON data in ajax and php

php - symfony - jquery 日期时间选择器

python - 如何用django创建一个私有(private)下载区?

linux - 我如何在 Linux VPS 上获得我的文件的直接下载链接

php - PHP 和 MYSQL 的复选框

android - 当从android上传图像文件到wcf时,图像文件被破坏