【发布时间】: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' => 0, 'filename2' => 1, ...]并保存为php serialize或json_encode -
@ChrisStrickland 是的,我知道如何循环遍历
$arr = explode("\n", $file); foreach ($arr as $key => $dat){ $arr[$key] = explode(":", $dat); }但问题是我不知道如何取出这些值并对它们进行操作,例如我想知道@987654330 中的哪一行@ 它是并更改$arr[][0]的值我被困在这一点上,这是我在这个冲刺中需要做的最后一件事......
标签: php search multidimensional-array