Я создаю страницу магазина для веб-сайта и хочу добавлять изображения, когда добавляю новые товары в базу данных. Я пытаюсь загрузить изображение в файл изображений только для изображений, а затем сохранить путь к изображению в виде строки в базе данных. затем вызвать строку из базы данных на странице магазина, чтобы показать изображение с помощью echo $ row ['' '].
сценарий сохраняет путь к целевому изображению в базе данных без проблем при тестировании, но изображение на самом деле не перемещается из своего местоположения tmp в место назначения.
Я приложил нижеприведенный сценарий, если кто-нибудь может указать мне правильное направление, я был бы очень благодарен!
<?php
$iName=$_POST['Name'];
$iDesc=$_POST['Description'];
$iFinish=$_POST['Finish'];
$iBrand=$_POST['Branding'];
$iPSLref=$_POST['Reference'];
$iAvail=$_POST['Availability'];
$file=$_FILES['image'];
if(!isset($_POST['formSubmit']))
{
// form accessed illegally
// header location
// exit
header("Location: ../../psl.index.php?page=manageItems&error=illegalAccess");
exit();
}
else
{
if(!preg_match("/^[a-zA-Z0-9\s]*$/", $iName))
{
// error
header("Location: ../../psl.index.php?page=manageItems&error=illegalName");
exit();
}
else
{
if(!preg_match("/^[a-zA-Z0-9\s]*$/", $iDesc))
{
// error
header("Location: ../../psl.index.php?page=manageItems&error=illegalDesc");
exit();
}
else
{
if(!preg_match("/^[a-zA-Z0-9\s]*$/", $iFinish))
{
// error
header("Location: ../../psl.index.php?page=manageItems&error=illegalFinish");
exit();
}
else
{
if(!preg_match("/^[a-zA-Z0-9\s]*$/", $iBrand))
{
// error
header("Location: ../../psl.index.php?page=manageItems&error=illegalBrand");
exit();
}
else
{
if(!preg_match("/^[a-zA-Z0-9\s]*$/", $iAvail))
{
// error
header("Location: ../../psl.index.php?page=manageItems&error=illegalAvail");
exit();
}
else
{
$fileName=$_FILES['image']['name'];
$fileTmpName=$_FILES['image']['tmp_name'];
$fileSize=$_FILES['image']['size'];
$fileError=$_FILES['image']['error'];
$fileType=$_FILES['image']['type'];
$fileExt=explode('.', $fileName);
$fileActExt=strtolower(end($fileExt));
$allowed=array('jpg', 'jpeg', 'png');
if(!in_array($fileActExt, $allowed))
{
// error
header("Location: ../../psl.index.php?page=manageItems&error=illegalExtention");
exit();
}
else
{
if($fileError!==0)
{
// error
header("Location: ../../psl.index.php?page=manageItems&error=filesError");
exit();
}
else
{
if($fileSize>5000000)
{
// error
header("Location: ../../psl.index.php?page=manageItems&error=exceededLimit");
exit();
}
else
{
$fileNameNew=uniqid('', true).'.'.$fileActExt;
$fileDestination="inc/images/".$fileNameNew;
include_once 'dbConn.script.php';
$sql="INSERT INTO item_data_table (name, description, finish, brand, PSLcode, availability, image) VALUES (?,?,?,?,?,?,?)";
$stmt=mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql))
{
//error
header("Location: ../../psl.index.php?page=manageItems&error=sqlError01");
exit();
}
else
{
mysqli_stmt_bind_param($stmt, "sssssss", $iName, $iDesc, $iFinish, $iBrand, $iPSLref, $iAvail, $fileDestination);
mysqli_stmt_execute($stmt);
move_uploaded_file($fileTmpName, $fileDestination);
header("Location: ../../psl.index.php?page=manageItems");
exit();
}
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
}
}
}
}
}
}
}
}
?>