【问题标题】:Optional file upload fields可选文件上传字段
【发布时间】:2017-09-06 05:58:33
【问题描述】:

在将数据插入 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); }
?>

【问题讨论】:

    标签: php mysql file-upload mysqli crud


    【解决方案1】:

    您可以使用它,从而摆脱您的错误。希望这对您有所帮助。

    $final_image = $image_type = $image_new_size = $final_file = $file_type = $file_new_size = "";
        if($_FILES) {         
                $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))
    
        }
    

    【讨论】:

    • 非常感谢!这看起来肯定对我有帮助。我只是无法将其放置在正确的位置,因此仍然出现错误。你建议我把它放在我的代码中的什么地方?
    • Mano- 此代码非常有用,但最后一件事...现在唯一的问题是,如果我只想上传“文件”而不是“图像”,则上传不起作用因为图像是我想的第一个条件......有什么想法吗?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2011-07-12
    • 2017-06-06
    • 1970-01-01
    • 2019-11-15
    • 2013-02-13
    相关资源
    最近更新 更多