【问题标题】:PHP Multidimensional Array Searching (edit and save results)PHP 多维数组搜索(编辑和保存结果)
【发布时间】:2019-01-11 04:22:45
【问题描述】:

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

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 中是否存在带有文件路径的行。接下来我想将: 之前的数字增加一,然后离开而不第二次将这个文件(图片)保存在文件夹中。我尝试了一些已经在这里发布的帖子,但我真的卡在这里了。

我坚持的数组示例:

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)

【问题讨论】:

  • file_exists() 是检查文件是否已存在的正确方法。我不确定我是否理解您的问题。
  • 我有一个 .dat 文件,其中的文件保存为 [points]:[filename],我想检查这个 .dat 文件是否存在并编辑 [points] 的数量
  • 而且您已经知道如何遍历文件。您只想弄清楚如何检查文件是否存在,如果存在则更改文件名?
  • 如果文件名是唯一的,您可以尝试创建一个数组为['filename1' =&gt; 0, 'filename2' =&gt; 1, ...]并保存为php serialize或json_encode
  • @ChrisStrickland 是的,我知道如何循环遍历$arr = explode("\n", $file); foreach ($arr as $key =&gt; $dat){ $arr[$key] = explode(":", $dat); } 但问题是我不知道如何取出这些值并对它们进行操作,例如我想知道@987654330 中的哪一行@ 它是并更改 $arr[][0] 的值我被困在这一点上,这是我在这个冲刺中需要做的最后一件事......

标签: php search multidimensional-array


【解决方案1】:

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

if (file_exists($target_file)) {

    $matches = false;
    $pattern = "~(\d+)(:".$target_file."$)~";
    $data = file_get_contents('database.dat');
    preg_match($pattern, $data, $matches);

    if($matches) {
        $oldLine = $matches[0];
        $newLine = ($matches[1]+1).$matches[2];
        $newData = str_replace($oldLine, $newLine, $data);
        file_put_contents('database.dat', $newData, LOCK_EX);
    }

}

这样做是你想要的吗?

【讨论】:

  • 你能解释一下它是如何工作的吗?我测试了它,数字没有改变0:uploads/Zdjęcie0232.jpg 0:uploads/Zdjęcie0232.jpg 0:uploads/Zdjęcie0232.jpg 0:uploads/Zdjęcie0228.jpg 0:uploads/Zdjęcie0262.jpg
  • 当然,哪一部分?正则表达式(preg_replace)还是数据更新?
  • 我真的不知道这是如何工作的,这种模式对我来说看起来并不不稳定
  • 模式只是 (\d+) 来查找一个数字然后:这是我们正在查找的行的一部分,然后是 $target_file,这是我们正在查找的文件,并且$ 表示行尾。你能 echo $newName 看看它包含什么吗?我想我看到了问题,我要编辑了。
  • @DzikaSuślica:现在检查。
猜你喜欢
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
相关资源
最近更新 更多