图像上传器适用于插入表单,但不适用于更新表单



我知道已经有很多类似的问题了,我提前为添加到文件中道歉,但我做研究的时间有点短,我需要快速帮助。我正在努力完成一项逾期未交的任务,当我添加产品时,我的图像上传功能运行良好,但当我更新产品时却没有。我不知道为什么。我更新图像的代码在这里:

require_once 'file-util.php'
// Check if the file exists before setting it
if (isset($_FILES['imageFile1'])) {
// Retrieve the name of the file based on what it was called on the client computer
$filename = $codeInput . '.png';
// Make sure the filename exists 
if (!empty($filename)) {
// Store the temporary location of where the file was stored on the server
$sourceLocation = $_FILES['imageFile1']['tmp_name'];
// Build the path to the images folder and use the same filename as before
$targetPath = $image_dir_path . DIRECTORY_SEPARATOR . $filename;
// Move file from temp directory to images folder
move_uploaded_file($sourceLocation, $targetPath);
}
}

这与我的insert_product文件中的代码完全相同。

我的file_util在这里:

$image_dir = 'images';
$image_dir_path = getcwd() . DIRECTORY_SEPARATOR . $image_dir;

其他一切都很完美,但只是这个小东西似乎没有任何作用,所以在我看来,在update_product中,我缺少了一些细节。我还需要做些什么才能让它发挥作用,还是我不知道的其他事情?

编辑:原来我只是忘记在add_product_form中设置加密类型。如果其他人有这个愚蠢的问题,请仔细检查你的表格,在身体顶部附近找到这个:

<form action="insert_product.php" method="post"
id="add_product_form"
enctype="multipart/form-data">

您需要检查更新表单标记是否具有正确的enctype属性值。。。请注意,要对上传的文件使用更多验证,您对文件名是否存在的检查将始终为真,因为您在前一行中为其设置了值。

显然,我的代码是对的,但我只是忘记了在update_product_form.php中执行"enctype="multipart/form-data"。

最新更新