Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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_Search_Multidimensional Array - Fatal编程技术网

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

PHP多维数组搜索(编辑和保存结果),php,search,multidimensional-array,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"]["n

我从一个文件中得到一个数组,如下所示

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();


?>
它是由上传制作的,看起来像这样

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();


?>
我想搜索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)

除非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);
    }

}

这是否符合您的要求?

file_exists()是检查文件是否已存在的正确方法。我不确定我是否理解你的问题。我有一个.dat文件,其中的文件保存为[points]:[filename],我想检查这个.dat文件是否存在,并编辑[points]的数量,你已经知道如何循环文件。您只想知道如何检查文件是否存在,如果存在,如何更改文件名?如果文件名是唯一的,您可以尝试创建一个数组作为
['filename1'=>0,'filename2'=>1,…]
,另存为或作为@christrilling,是的,我知道如何通过
$arr=explode(“\n,$file”)循环;foreach($arr as$key=>$dat){$arr[$key]=explode(“:”,$dat);}
但问题是我不知道如何从这些值中取出并对它们进行操作,例如,我想知道
$arr[][
中的哪一行,并更改
$arr[][0]的值
我在这一点上被绊住了,这是我在这次冲刺中需要做的最后一件事……你能解释一下它是如何工作的吗?我测试了它,数字没有变化
0:uploads/Zdjęcie0232.jpg 0:uploads/Zdjęcie0232.jpg 0:uploads/Zdjęcie0232.jpg 0:uploads/Zdjęcie0222.jpg
确定,哪个部分?正则表达式(preg\u replace)或数据更新?我真的不知道这是如何工作的,这个模式看起来不太容易理解。模式只是(\d+)寻找一个数字,然后是我们正在寻找的行的一部分,然后是$target\u文件,这是我们正在寻找的文件,以及$which标志着行的结束。您能回显$newName以查看它包含的内容吗?我想我看到问题了,我要编辑了。@DzikaSuślica:现在检查一下。