Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP使用已知文件名的一部分移动文件_Php_Mysql - Fatal编程技术网

PHP使用已知文件名的一部分移动文件

PHP使用已知文件名的一部分移动文件,php,mysql,Php,Mysql,我有一个目录的图像(40000+),我需要排序的充分。我已经设计了一个脚本来将它们排序到正确的目录中,但是,我对文件名有问题 带有它们所属id的图像URL存储在数据库中,我将数据库与脚本结合使用来对图像进行排序 我的问题是: 数据库中的图像url被缩短。此类对应图像的示例如下所示: dsc_0107-367.jpg dsc_0107-367-5478-2354-0014.jpg 文件名的第一部分相同,但实际文件包含更多信息。我想用文件名的已知部分从数据库中移动文件 我有一个基本代码: <

我有一个目录的图像(40000+),我需要排序的充分。我已经设计了一个脚本来将它们排序到正确的目录中,但是,我对文件名有问题

带有它们所属id的图像URL存储在数据库中,我将数据库与脚本结合使用来对图像进行排序

我的问题是:

数据库中的图像url被缩短。此类对应图像的示例如下所示:

dsc_0107-367.jpg
dsc_0107-367-5478-2354-0014.jpg
文件名的第一部分相同,但实际文件包含更多信息。我想用文件名的已知部分从数据库中移动文件

我有一个基本代码:

<?php

$sfiles = mysqli_query($dbconn, "SELECT * FROM files WHERE gal_id = '$_GET[id']");

while($file = mysqli_fetch_assoc($sfiles)){

$folder = $file['gal_id'];
$fileToMove = $file['filename'];

$origDir = "mypath/to/dir";

$newDir = "mypath/to/new/dir/$file['gal_id']";

mkdir "$newDir";

mv "$fileToMove" "$newDir";

}

您的MySQL查询已经成熟,可以进行SQL注入,如果我访问您的页面时遇到类似以下内容,则需要对GET语句进行清理:

pagename.php?id=' DROP TABLE; #--
这会给你带来非常糟糕的结局

所以,

总的来说,使用它要好得多。关于如何在世界各地和更广泛的互联网上使用它们,有很多很多数据。我在下面展示的只是一个权宜之计

$id = (int)$_GET['id']  //This forces the id value to be numeric.
$sfiles = mysqli_query($dbconn, "SELECT * FROM files WHERE gal_id = ".$id);
还要注意关闭
引号,因为您的原始引号没有关闭数组键包装器引号

我从未使用过
mysqli\u fetch\u assoc
,一直使用
mysqli\u fetch\u array
,因此将使用它,因为它符合相同的语法:

while($file = mysqli_fetch_array($sfiles)){
     $folder = $id //same thing. 
     $fileToMove = $file['filename'];
     $origDir = "mypath/to/dir/".$fileToMove;
     //This directory shold always start with Server['DOCUMENT_ROOT']. 
     //Please read the manual for it. 
     $newDir =  $_SERVER['DOCUMENT_ROOT']."/mypath/to/new/dir/".$folder;

     if(!is_dir($newDir)){
     mkdir $newDir;
     }

    // Now the magic happens, copies the file to the new directory.
    // Then (optionally) delete the original. 
    copy($origDir,$newDir."/".$fileToMove);
    unlink($origDir); //removes original.

    // Add a flag to your Database to know that this file has been copied, 
    // ideally you should resave the filepath to the correct new one. 
    //MySQL update saving the new filepath. 
    }
仔细阅读并阅读


和;请使用准备好的语句进行PHP和数据库交互。!PHP可以使用函数
glob()
打开文件。glob将搜索您的服务器或指定目录,以查找包含与您指定的模式“匹配”的任何文件

像这样使用
glob()
将从部分名称中提取图像

  • 将此查询与第二个查询分开运行:

    $update = mysqli($dbconn, "UPDATE files 
              SET filename = REPLACE(filename, '.info', ''));
    
  • filename
    应该是数据库中包含图像列表的列。我们从db列中删除.jpg的原因是,如果您的名称是部分的,.jpg可能与目录中的给定名称不匹配。删除后,我们可以只搜索名称的模式

  • 生成查询以选择和移动文件夹:

     $sfiles = mysqli_query($dbconn, "SELECT * FROM files");
    
     while($file = mysqli_fetch_assoc($sfiles)){
    
     $fileToMove = $file['filename'];
    
     // because glob outputs the result set into an array, 
     // we will use foreach to run each result from the array individually.
    
     foreach(glob("$fileToMove*") as filename){
    
     echo "$filename <br>"; 
     // I'm echoing this out to see that the results are being run 
     // one line at a time and to confirm the photo's are 
     // matching the pattern.
    
     $folder = $file['gal_id']; 
     // pulling the id from the db of the gallery the photo belongs to. 
     // This will specify which folder to move the pic to. 
     // Replace gal_id with the name of your column.
    
     $newDir = $_SERVER['DOCUMENT_ROOT']."/admin/wysiwyg/kcfinder/upload/images/gallery/old/".$folder;
    
     copy($filename,$newDir."/".$filename); 
     // I would recommend copy rather than move. 
     // This will leave the original photo in its place. 
     // This measure is to ensure the photo made it to the new directory so you don't lose it.
     //  You could go back and delete the photos after if you'd prefer.
    
         }
    
    
     }
    
    $sfiles=mysqli_查询($dbconn,“从文件中选择*);
    而($file=mysqli\u fetch\u assoc($sfiles)){
    $fileToMove=$file['filename'];
    //因为glob将结果集输出到数组中,
    //我们将使用foreach分别运行数组中的每个结果。
    foreach(glob($fileToMove*)作为文件名){
    回显“$filename
    ”; //我重复这一点是为了看看结果是否正在运行 //一次一行,并确认照片的 //与模式匹配。 $folder=$file['gal_id']; //正在从照片所属库的数据库中提取id。 //这将指定将pic移动到哪个文件夹。 //将gal_id替换为列的名称。 $newDir=$_SERVER['DOCUMENT_ROOT'].“/admin/wysiwyg/kcfinder/upload/images/gallery/old/”$folder; 复制($filename,$newDir.“/”$filename); //我建议复制而不是移动。 //这将保留原始照片的位置。 //这项措施是为了确保照片进入新目录,这样你就不会丢失它。 //如果您愿意,您可以在之后返回并删除这些照片。 } }

  • SQL列有一个你需要注意的长度属性。设置一个更大的长度,而不是12。我们开始写一些东西,如果你遇到问题,我们会很乐意帮助你。不过,是这样!=在这里找到你的免费编码器我已经更新了我的代码。我不想找一个免费的编码器。我非常乐意编写代码。我想知道如何选择和移动文件的一部分,有12个字符,我知道文件名较长。@RiggsFollyWARNING:当使用
    mysqli
    时,您应该使用and将用户数据添加到查询中。不要使用字符串插值或连接来完成此操作,因为您创建了一个严重的字符串。切勿将
    $\u POST
    $\u获取
    数据直接输入o一个查询,如果有人试图利用您的错误进行攻击,它可能会非常有害。请戳一戳该函数,看看您得到了多少。我只有文件名的前12个字符。在这种情况下,这仍然会移动文件吗?此答案不能回答移动部分文件名匹配的文件的问题。@Martin对于code。我不知道为什么它没有把它放在代码格式中。要点[no.1.,no.2.](即使它们不是HTML格式的项目符号),使代码需要8个空格而不是4个空格才能成为代码块。