图片上传器非常适合插入表单,但不适用于更新表单
Image uploader working perfectly for an insert form but not for an update form
我知道已经有很多类似的问题,对于添加到文件中,我提前表示歉意,但我做研究的时间有点短,我需要快速帮助。我正在尝试完成一项逾期的作业,并且我的图片上传功能在我添加产品时运行良好,但在我更新产品时却无法正常运行。我不知道为什么。我更新图片的代码在这里:
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 中设置加密类型。如果其他人有这个愚蠢的问题,请仔细检查 body:
顶部附近的表格
<form action="insert_product.php" method="post"
id="add_product_form"
enctype="multipart/form-data">
您需要检查您更新的表单标签是否具有正确的 enctype 属性值...
并且请注意对上传的文件使用更多验证,您对文件名是否存在的检查将始终为真,因为您在上一行中为其设置了值。
显然,我的代码是正确的,但我只是忘了去 "enctype="multipart/form-data" in update_product_form.php.
我知道已经有很多类似的问题,对于添加到文件中,我提前表示歉意,但我做研究的时间有点短,我需要快速帮助。我正在尝试完成一项逾期的作业,并且我的图片上传功能在我添加产品时运行良好,但在我更新产品时却无法正常运行。我不知道为什么。我更新图片的代码在这里:
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 中设置加密类型。如果其他人有这个愚蠢的问题,请仔细检查 body:
顶部附近的表格<form action="insert_product.php" method="post"
id="add_product_form"
enctype="multipart/form-data">
您需要检查您更新的表单标签是否具有正确的 enctype 属性值... 并且请注意对上传的文件使用更多验证,您对文件名是否存在的检查将始终为真,因为您在上一行中为其设置了值。
显然,我的代码是正确的,但我只是忘了去 "enctype="multipart/form-data" in update_product_form.php.