PDA

View Full Version : سوال: آپلود تصویر



php_developer
جمعه 13 اسفند 1395, 14:20 عصر
سلام
من سه تا input[type="file"]
دارم حالا تو قسمت ویرایش میخوام یک عکس مجددا آپلود کنم
و با اون دو تا عکس قبلی کاری ندارم که تو دیتابیس ثبت کردم
ولی زمانی که به عنوان مثال باinput اول عکسی رو آپلود میکنم و
فرمم رو submit میکنم اون دو تا input دیگه هم مقادیر خالی ارسال
می کنند و مقدار هایی که قبلا تو دیتابیس ثبت کردم رو پاک میکنن
چیکار کنم که این اتفاق نیفته یعنی اگه چیزی آپلود نکردم و submit
کردم فرم رو اتفاقی نیوفته و مقدار خالی ارسال نکنه

ممنون میشم راهنماییم کنید

thacker
جمعه 13 اسفند 1395, 14:29 عصر
لطفا کدهای برنامتون رو اینجا قرار بدید

php_developer
جمعه 13 اسفند 1395, 19:08 عصر
if ($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['form_name'] == "editTv") {
$id = check_value($_POST['id']);
$name = check_value($_POST['name']);
$price = check_value($_POST['price']);
$model = check_value($_POST['model']);
$item_id = check_value($_POST['item_id']);
$display_type = check_value($_POST['display_type']);
$size = check_value($_POST['size']);
$off = check_value($_POST['off']);
$new = check_value($_POST['new']);
$resolution = check_value($_POST['resolution']);
$tv_the_screen = check_value($_POST['tv_the_screen']);
$tv_compliance_picture = check_value($_POST['tv_compliance_picture']);
$tv_Deep_black_color = check_value($_POST['tv_Deep_black_color']);
$tv_set_spot_light = check_value($_POST['tv_set_spot_light']);
$tv_Quantum_dot_technology = check_value($_POST['tv_Quantum_dot_technology']);
$tv_HDR = check_value($_POST['tv_HDR']);
$tv_Super_Black_Panel = check_value($_POST['tv_Super_Black_Panel']);
$tv_Increase_automatic_depth = check_value($_POST['tv_Increase_automatic_depth']);
$tv_The_highest_brightness = check_value($_POST['tv_The_highest_brightness']);
$tv_einforcing_color_picker = check_value($_POST['tv_einforcing_color_picker']);
$tv_play_3d = check_value($_POST['tv_play_3d']);
$tv_motion_control = check_value($_POST['tv_motion_control']);
$tv_upgradable = check_value($_POST['tv_upgradable']);
$tv_processor = check_value($_POST['tv_processor']);
$tv_SmartTV = check_value($_POST['tv_SmartTV']);
$tv_operating_system_Tizen = check_value($_POST['tv_operating_system_Tizen']);
$tv_Smart_reflection = check_value($_POST['tv_Smart_reflection']);
$tv_smart_view = check_value($_POST['tv_smart_view']);
$tv_smart_Sound_Reflection = check_value($_POST['tv_smart_Sound_Reflection']);
$tv_Multi_function_remote_control = check_value($_POST['tv_Multi_function_remote_control']);
$tv_one_connect = check_value($_POST['tv_one_connect']);
$tv_TV_Family = check_value($_POST['tv_TV_Family']);
$tv_The_slim_design = check_value($_POST['tv_The_slim_design']);
$tv_internal_wifi = check_value($_POST['tv_internal_wifi']);
$tv_sport_mode = check_value($_POST['tv_sport_mode']);
$tv_HDMI = check_value($_POST['tv_HDMI']);
$tv_usb = check_value($_POST['tv_usb']);
$tv_width_x_height_x_thickness = check_value($_POST['tv_width_x_height_x_thickness']);
$tv_description = check_value($_POST['tv_description']);
$error = false;


$file_name = $_FILES['img1']['name'];
$file_tmpname = $_FILES['img1']['tmp_name'];
$file_size = $_FILES['img1']['size'];

if ($file_size > 0) {
$target_dir = "../../../images/TV/";
$target_file = $target_dir . basename($file_name);
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

if ($file_size > 1000000) { /* 1MB */
echo "<span style=\"color: red;\">ببخشید، حجم عکس ارسالی شما زیاد است</span>";
$error = true;
} elseif ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
echo "<span style=\"color: red;\">ببخشید، فرمت فایل ارسالی غیر مجاز است </span>";
$error = true;
}

if (empty($error)) {
$file_name = strtolower($file_name);
$target_file = $target_dir . basename($file_name);
$prefix = md5(mt_rand(111111111, 999999999) . uniqid());
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$file_name = $prefix . '.' . $imageFileType;
$target_file = $target_dir . basename($file_name);

move_uploaded_file($file_tmpname, $target_file);
$filename = $file_name;
}
} else {
}

$file_name = $_FILES['img2']['name'];
$file_tmpname = $_FILES['img2']['tmp_name'];
$file_size = $_FILES['img2']['size'];

if ($file_size > 0) {
$target_dir = "../../../images/TV/";
$target_file = $target_dir . basename($file_name);
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

if ($file_size > 1000000) { /* 1MB */
echo "<span style=\"color: red;\">ببخشید، حجم عکس ارسالی شما زیاد است</span>";
$error = true;
} elseif ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
echo "<span style=\"color: red;\">ببخشید، فرمت فایل ارسالی غیر مجاز است </span>";
$error = true;
}

if (empty($error)) {
$file_name = strtolower($file_name);
$target_file = $target_dir . basename($file_name);
$prefix = md5(mt_rand(111111111, 999999999) . uniqid());
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$file_name = $prefix . '.' . $imageFileType;
$target_file = $target_dir . basename($file_name);

move_uploaded_file($file_tmpname, $target_file);
$filename1 = $file_name;
}
} else {
}

$file_name = $_FILES['img3']['name'];
$file_tmpname = $_FILES['img3']['tmp_name'];
$file_size = $_FILES['img3']['size'];

if ($file_size > 0) {
$target_dir = "../../../images/TV/";
$target_file = $target_dir . basename($file_name);
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

if ($file_size > 1000000) { /* 1MB */
echo "<span style=\"color: red;\">ببخشید، حجم عکس ارسالی شما زیاد است</span>";
$error = true;
} elseif ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
echo "<span style=\"color: red;\">ببخشید، فرمت فایل ارسالی غیر مجاز است </span>";
$error = true;
}

if (empty($error)) {
$file_name = strtolower($file_name);
$target_file = $target_dir . basename($file_name);
$prefix = md5(mt_rand(111111111, 999999999) . uniqid());
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$file_name = $prefix . '.' . $imageFileType;
$target_file = $target_dir . basename($file_name);

move_uploaded_file($file_tmpname, $target_file);
$filename2 = $file_name;
}
} else {
}

if ($file_size > 0) {
if (empty($error)) {
$sql = "UPDATE `product_tv` SET
`name`='$name',
`price`='$price',
`model`='$model',
`item_id`='$item_id',
`image`='$filename',
`image2`='$filename1',
`image3`='$filename2',
`display_type`='$display_type',
`size`='$size',
`off`='$off',
`new`='$new',
`resolution`='$resolution',
`tv_the_screen`='$tv_the_screen',
`tv_compliance_picture`='$tv_compliance_picture',
`tv_Deep_black_color`='$tv_Deep_black_color',
`tv_set_spot_light`='$tv_set_spot_light',
`tv_Quantum_dot_technology`='$tv_Quantum_dot_techn ology',
`tv_HDR`='$tv_HDR',
`tv_Super_Black_Panel`='$tv_Super_Black_Panel',
`tv_Increase_automatic_depth`='$tv_Increase_automa tic_depth',
`tv_The_highest_brightness`='$tv_The_highest_brigh tness',
`tv_einforcing_color_picker`='$tv_einforcing_color _picker',
`tv_play_3d`='$tv_play_3d',
`tv_motion_control`='$tv_motion_control',
`tv_upgradable`='$tv_upgradable',
`tv_processor`='$tv_processor',
`tv_SmartTV`='$tv_SmartTV',
`tv_operating_system_Tizen`='$tv_operating_system_ Tizen',
`tv_Smart_reflection`='$tv_Smart_reflection',
`tv_smart_view`='$tv_smart_view',
`tv_smart_Sound_Reflection`='$tv_smart_Sound_Refle ction',
`tv_Multi_function_remote_control`='$tv_Multi_func tion_remote_control',
`tv_one_connect`='$tv_one_connect',
`tv_TV_Family`='$tv_TV_Family',
`tv_The_slim_design`='$tv_The_slim_design',
`tv_internal_wifi`='$tv_internal_wifi',
`tv_sport_mode`='$tv_sport_mode',
`tv_HDMI`='$tv_HDMI',
`tv_usb`='$tv_usb',
`tv_width_x_height_x_thickness`='$tv_width_x_heigh t_x_thickness',
`tv_description`='$tv_description'
WHERE `id` = '$id'";

$result = $db->query($sql);
if ($result) {
header("Location: ../index.php?mid=tv&success_upload_img=OK");
} else {
echo "<span style=\" color: red\">ثبت محصول با مشکل مواجه شده است</span>";
}
}
} elseif ($file_size == 0) {
if (empty($error)) {
$sql = "UPDATE `product_tv` SET
`name`='$name',
`price`='$price',
`model`='$model',
`item_id`='$item_id',
`display_type`='$display_type',
`size`='$size',
`off`='$off',
`new`='$new',
`resolution`='$resolution',
`tv_the_screen`='$tv_the_screen',
`tv_compliance_picture`='$tv_compliance_picture',
`tv_Deep_black_color`='$tv_Deep_black_color',
`tv_set_spot_light`='$tv_set_spot_light',
`tv_Quantum_dot_technology`='$tv_Quantum_dot_techn ology',
`tv_HDR`='$tv_HDR',
`tv_Super_Black_Panel`='$tv_Super_Black_Panel',
`tv_Increase_automatic_depth`='$tv_Increase_automa tic_depth',
`tv_The_highest_brightness`='$tv_The_highest_brigh tness',
`tv_einforcing_color_picker`='$tv_einforcing_color _picker',
`tv_play_3d`='$tv_play_3d',
`tv_motion_control`='$tv_motion_control',
`tv_upgradable`='$tv_upgradable',
`tv_processor`='$tv_processor',
`tv_SmartTV`='$tv_SmartTV',
`tv_operating_system_Tizen`='$tv_operating_system_ Tizen',
`tv_Smart_reflection`='$tv_Smart_reflection',
`tv_smart_view`='$tv_smart_view',
`tv_smart_Sound_Reflection`='$tv_smart_Sound_Refle ction',
`tv_Multi_function_remote_control`='$tv_Multi_func tion_remote_control',
`tv_one_connect`='$tv_one_connect',
`tv_TV_Family`='$tv_TV_Family',
`tv_The_slim_design`='$tv_The_slim_design',
`tv_internal_wifi`='$tv_internal_wifi',
`tv_sport_mode`='$tv_sport_mode',
`tv_HDMI`='$tv_HDMI',
`tv_usb`='$tv_usb',
`tv_width_x_height_x_thickness`='$tv_width_x_heigh t_x_thickness',
`tv_description`='$tv_description'
WHERE `id` = '$id'";

$result = $db->query($sql);
if ($result) {
header("Location: ../index.php?mid=tv");
} else {
echo "<span style=\" color: red\">ثبت محصول با مشکل مواجه شده است</span>";
}
}
}
}

thacker
شنبه 14 اسفند 1395, 11:58 صبح
با توضیحاتی که شما دادی توی کوئری آپدیت نیازی به آپدیت image2 , image3 نیست و میتوانید این دو خط رو از کدها حذف کنید

php_developer
شنبه 14 اسفند 1395, 15:36 عصر
شاید خواسنم به جای image1
مثلا image2 آپلود کنم و 1و3 رو خالی بذارم
اونوقت چیکار کنم
با این حساب مجبورم دوباره یه کوئری دیگه بزنم مثلا با image 2
1و3 رو پاک کنم
میخوام یه جوری باشه که زمانی که اون دو تای دیگه اپلود نمیشه مقدار قبلی تو db بمونه پاک نشه

thacker
شنبه 14 اسفند 1395, 15:46 عصر
$file_name = $_FILES['img1']['name'];
$fileName2 = $_FILES['img2']['name'];
$fileName3 = $_FILES['img3']['name'];

با تابع empty() چک کنید کدام فیلدها خالی است. در صورتی که خالی بود از دیتابیس کوئری بگیرید و همان مقدار قبلی image را جایگزین کنید

php_developer
یک شنبه 15 اسفند 1395, 08:41 صبح
میشه بیشتر توضیح بدید عذر میخوام سوال زیاد میکنم
در قالب مثال میتونید بهم توضیح بدید چکار کنم
قبل اینکه با empty چک کنم باید به دیتابیس کودری بزنم
لطفا اگه میشه یه مثال بیارید بهتر بفهمم
ممنونم

thacker
یک شنبه 15 اسفند 1395, 10:47 صبح
$image = $_FILES['img1']['name'];
$image2 = $_FILES['img2']['name'];


if(empty($image2) || !isset($image2) || is_null($image2)) {

$query = mysql_query("SELECT `image2` FROM `products` WHERE `id` = '$id' LIMIT 1");
$row = mysql_fetch_array($query);

$img2 = $row['image2'];

} else {

$img2 = $file_name;

}


$sql = "UPDATE `products` SET `image2` = '$img2' WHERE `id` = '$id'";

php_developer
یک شنبه 15 اسفند 1395, 10:51 صبح
خیلی لطف کردید ممنونم