2017-04-11 8 views
1

mySQL dbにデータを挿入し、そのファイルをサーバーにアップロードする際に、2つのファイルアップロードをオプションにしようとしています。両方のファイルを新しいエントリにアップロードすると、アップロードは成功します。 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

これを使用すると、エラーを取り除くことができます。これが役立ちます。

$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)) 

    } 
+0

ありがとうございました!これは私を助けるように確信しています。私はちょうど正しい場所に配置することに問題があるので、まだエラーが発生しています。私はこれを私のコードの中に入れようと提案していますか? – surfingpig

+0

また、ありがとうございます。 – surfingpig

+0

心配しないでください!ありがとうございました! – surfingpig

関連する問題