PHP多维数组搜索(编辑和保存结果)

时间:2019-01-11 04:22:46

标签: php search multidimensional-array

所以我有一个看起来像这样的文件数组

0:uploads/Zdjęcie0232.jpg
0:uploads/Zdjęcie0222.jpg
0:uploads/duparomana.jpg
0:uploads/dddfsdfsd.jpg
0:uploads/6.jpg

它是由看起来像这样的上载

<?php
$licznik = 0;
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {

}
/* Check file size

}*/
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file//
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

        // open file to read
        $fp = fopen("database.dat", "r");   
        $separator=':';
        //read data
        $stareDane = fread($fp, filesize("database.dat"));
        // close file
        fclose($fp);
        // create new data
        $noweDane = $licznik . $separator . $target_file;
        $noweDane .= PHP_EOL . $stareDane;
        // open file to write
        $fp = fopen("database.dat", "w");
        // save data
        fputs($fp, $noweDane);
        // close file
        fclose($fp);
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

echo "Zaraz nastąpi przekierowanie";
sleep (3);
header("Location: /index.php?filename=$target_file", true, 301);
exit();


?>

因此在

部分
// Check if file already exists
if (file_exists($target_file)) {

}

我想搜索database.dat中是否存在文件路径。接下来,我想将:之前的数字增加1,然后在第二次不保存此文件(图片)的情况下离开文件夹。我尝试了一些已经在这里发布的帖子,但我确实停留在这里。

我坚持的数组示例:

array (size=24)   0 => 
    array (size=2)
      0 => string '0' (length=1)
      1 => string 'uploads/Zdjęcie0232.jpg ' (length=25)   1 => 
    array (size=2)
      0 => string '0' (length=1)
      1 => string 'uploads/Zdjęcie0228.jpg ' (length=25)   2 => 
    array (size=2)
      0 => string '0' (length=1)
      1 => string 'uploads/Zdjęcie0262.jpg ' (length=25)

1 个答案:

答案 0 :(得分:0)

除非database.dat非常大,否则我可能只是将整个文件读入内存并对其进行替换,然后覆盖。您可以尝试这样的事情。它使用正则表达式查找数据并计算新数字。

sun.misc

那是你想要的吗?