没有mysql的Php图像和描述上传


Php image and description uploading without mysql

我正在尝试编写一种图像上传脚本,该脚本将上传文件并为每个上传的图像写一行,其中包含描述和所有内容。现在,如果我使用 $i++ 函数,它将始终写入 $i=0,因为没有 foreach 标签,我真的不知道是否可以在没有数据库等的情况下将 foreach 标签用于这种脚本。

if ($_FILES[image][size] > 0) {
    $ext = end(explode(".", $_FILES[image][name]));
    if (strtolower($ext) == 'jpg' || strtolower($ext) == 'png' || strtolower($ext) == 'bmp') {
        $tmp     = $_FILES[image][tmp_name];
        $new     = "$folder/" . time() . "_" . base64_encode($_POST[filename]) . "_" . base64_encode($_FILES[image][name]) . ".$ext";
        $p_name  = $_POST[project_name];
        $p_about = $_POST[project_about];
        file_put_contents('images.txt', $new . "'r'n", FILE_APPEND);
        move_uploaded_file($tmp, $new);
    }
    header('Location: index.php');
    die;
 }

尚未使用 $p_name 和 $p_about。我想要的是每行写一个唯一的ID,而不是项目名称,而不是关于项目和图像链接,以便在您通过我必须编写的功能删除图像时删除特定行。

你可以使用 json 来做这样的事情。

<?php
$storage_file = './path/to/storage.json';
touch($storage_file);
$storage = file_get_contents($storage_file);
$storage = json_decode($storage,true);
$storage = empty($storage) ? array() : $storage;
$new_record = array();
$new_record['p_name'] = $_POST[project_name];
$new_record['p_about'] = $_POST[project_about];
$new_record['file'] = $new;
$new_id = count($storage);
$storage[] = $new_record;
file_put_contents($storage_file,json_encode($storage));
echo 'New ID: '.$new_id;
?>