可选的文件上载字段

时间:2017-04-11 02:40:19

标签: php mysql file-upload mysqli crud

我想在将数据插入mySQL数据库并将文件上传到我的服务器时,使我的两个文件上传成为可选项。当我将两个文件上传到新条目时,上传成功。如果我没有上传1个或两个文件,我收到错误。非常感谢你的帮助。

<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/session.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/db_connection.php");?>
<?php
session_start();
if($_SESSION["login_user"] != true) {
    echo("Access denied!");
    exit();
}
?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/functions.php");?>
<?php require_once($_SERVER['DOCUMENT_ROOT']."/includes/validation_functions.php");?>
<?php
if (isset($_POST['submit'])) {
    // Process the form
        $visible = mysqli_prep($_POST["visible"]);
        $homepage = mysqli_prep($_POST["homepage"]);
        $type = mysqli_prep($_POST["type"]);
        $publication_name = mysqli_prep($_POST["publication_name"]);
        $publication_url = mysqli_prep($_POST["publication_url"]);
        $month = mysqli_prep($_POST["month"]);
        $date = mysqli_prep($_POST["date"]);
        $year = mysqli_prep($_POST["year"]);
        $title = mysqli_prep($_POST["title"]);
        $author = mysqli_prep($_POST["author"]);
        $summary = mysqli_prep($_POST["summary"]);
        $full_text = mysqli_prep($_POST["full_text"]);
        $tag_1 = mysqli_prep($_POST["tag_1"]);
        $tag_2 = mysqli_prep($_POST["tag_2"]);
        $tag_3 = mysqli_prep($_POST["tag_3"]);
        $tag_4 = mysqli_prep($_POST["tag_4"]);
        $tag_5 = mysqli_prep($_POST["tag_5"]);
        $tag_6 = mysqli_prep($_POST["tag_6"]);
        $tag_7 = mysqli_prep($_POST["tag_7"]);
        $image = rand(1000,100000)."-".$_FILES['image']['name'];
        $image_loc = $_FILES['image']['tmp_name'];
        $image_size = $_FILES['image']['size'];
        $image_type = $_FILES['image']['type'];
        $image_folder="images/";
        $file = rand(1000,100000)."-".$_FILES['file']['name'];
        $file_loc = $_FILES['file']['tmp_name'];
        $file_size = $_FILES['file']['size'];
        $file_type = $_FILES['file']['type'];
        $file_folder="files/";

 $image_new_size = $image_size/1024;  
 $file_new_size = $file_size/1024;  
 $new_image_name = strtolower($image);
 $new_file_name = strtolower($file);


 $final_image=str_replace(' ','-',$new_image_name);
 $final_file=str_replace(' ','-',$new_file_name);

 if(move_uploaded_file($image_loc,$image_folder.$final_image))
 if(move_uploaded_file($file_loc,$file_folder.$final_file))


    $query  = "INSERT INTO `news` (";
    $query .= "visible, homepage, type, publication_name, publication_url, month, date, year, title, author, summary, full_text, tag_1, tag_2, tag_3, tag_4, tag_5, tag_6, tag_7, image, image_type, image_size, file, file_type, file_size ";
    $query .= ") VALUES (";
    $query .= "  '{$visible}', '{$homepage}', '{$type}', '{$publication_name}', '{$publication_url}', '{$month}', '{$date}', '{$year}', '{$title}', '{$author}', '{$summary}', '{$full_text}', '{$tag_1}', '{$tag_2}', '{$tag_3}', '{$tag_4}', '{$tag_5}', '{$tag_6}', '{$tag_7}', '{$final_image}','{$image_type}','{$image_new_size}', '{$final_file}','{$file_type}','{$file_new_size}'";
    $query .= ")";
    $result = mysqli_query($connection, $query);

    if ($result) {
        // Success
        $_SESSION["message"] = "Item created.";
        redirect_to("manage_content.php");
    } else {
        // Failure
        //$_SESSION["message"] = "Item creation failed.";
        //redirect_to("new_news.php");
        echo "Error: " . $query . "<br>" . $result->error;
    }



} else {
    // This is probably a GET request
    redirect_to("new_news.php");
}

?>

<?php
    if (isset($connection)) { mysqli_close($connection); }
?>

1 个答案:

答案 0 :(得分:1)

你可以使用它,因此摆脱你的错误。希望这可以帮助你。

yield*
相关问题