2017-05-27 73 views
-1

我試過一些代碼來保存我上傳的圖像在文件夾中。文件夾的路徑是C:\xampp\htdocs\gripOffers\Store_Brand\store_admin\images。我無法將其保存在文件夾中。這個怎麼做?我在哪裏做錯了?已上傳的圖像移動到文件夾 - php

代碼

if(isset($_POST['submit'])) { 
    $image = $_FILES['file']['name']; 

    $temp_name = $_FILES["file"]["tmp_name"]; 

    $allowedExts = array("gif", "jpeg", "jpg", "png"); 
    $type = pathinfo($image, PATHINFO_EXTENSION); 
    echo "<br>"; 
    echo $type; 
    echo "<br>"; 

    if ((($type == "image/gif") 
      || ($type == "jpeg") 
      || ($type == "jpg") 
      || ($type == "pjpeg") 
      || ($type == "x-png") 
      || ($type == "png")) 
     && ($_FILES["file"]["size"] < 50000) 
    ) { 
     $m = move_uploaded_file($_FILES["file"]["tmp_name"], "/Store_Brand/store_admin/images/" . $image); 
     if ($m) { 
      echo "uploaded!"; 
     } else { 
      echo "no"; 
     } 
    } else { 
     echo "file type is not image"; 
    } 
} 
+0

嘗試添加'DOCUMENT_ROOT'到目標路徑。 –

+0

@u_mulder在哪一行? – tabia

+1

@tabia,'move_uploaded_file' - 那一個。檢查該目錄的權限也無妨。 – Pyromonk

回答

0

變化的<Form action="">和嘗試......

<!DOCTYPE html> 
<html> 
<body> 

<form action="example.php" method="post" enctype="multipart/form-data"> 
    Select image to upload: 
    <input type="file" name="file" id="file"> 
    <input type="submit" value="Upload Image" name="submit"> 
</form> 

</body> 
</html> 


<?php 

error_reporting(0); 

if(isset($_POST['submit'])) { 

    $image_name = $_FILES['file']['name']; 

    $temp_name = $_FILES["file"]["tmp_name"]; 

    $file_extension = strtolower(end(explode('.',$_FILES['file']['name']))); 

    $file_size =$_FILES['file']['size']; 

     $expensions = array("jpeg","jpg","png","gif"); 

     if(in_array($file_extension,$expensions)=== false){ 

     $message = "File Type Not allowed, Please choose a JPEG or PNG file."; 
     } 

     if($file_size > 500000){ 

     $message = '<h1> File size Too Large !! </h1>'; 
     } 

     if(empty($message)==true){ 

     move_uploaded_file($temp_name,$_SERVER['DOCUMENT_ROOT']."/gripOffers/Store_Brand/store_admin/images/".$image_name); 

     $message = "<h1> Your file upload successfully. </h1>"; 
     } 
     else{ 

     $message = "<h1> Something is Wrong </h1>"; 
     } 
} 


if(isset($message)) 
{ 
echo '<div style="color:#FF0000;text-align:center;font-size:12px;">'.$message.'</div>'; 
} 

?> 
+0

什麼都沒有發生!! – tabia

+0

好吧等待...... –

+0

@tabia現在,你可以嘗試... –

1

不要忘記在表單標籤指定enctype="multipart/form-data"

$_SERVER['DOCUMENT_ROOT']是回由 'DocumentRoot的' 指令

<form method="post" enctype="multipart/form-data"> 
    <input type="file" name="file" value=""> 
    <input type="submit" name="submit" value="submit"> 
</form> 

PHP代碼

<?php 
if(isset($_POST['submit'])) { 
    $image = $_FILES['file']['name']; 

    $temp_name = $_FILES["file"]["tmp_name"]; 

    $file_ext=strtolower(end(explode('.',$_FILES['file']['name']))); 
    $file_size =$_FILES['file']['size']; 
     $expensions= array("jpeg","jpg","png","gif"); 

     if(in_array($file_ext,$expensions)=== false){ 
     $errors[]="extension not allowed, please choose a JPEG or PNG file."; 
     } 

     if($file_size > 2097152){ 
     $errors[]='File size must be excately 2 MB'; 
     } 

     if(empty($errors)==true){ 
     move_uploaded_file($temp_name,$_SERVER['DOCUMENT_ROOT']."/"."gripOffers/Store_Brand/store_admin/images/".$image); 
     echo "Your file upload successfully."; 
     }else{ 
     print_r($errors); 
     } 
} 
?> 

定義項目的根目錄下也許它會幫助你

+0

您的代碼相呼應,文件上傳成功,但是當我檢查的圖像文件夾中沒有圖像。 – tabia

+0

你'images'文件夾權限讀取寫入數據?首先檢查 – Nidhi

+0

是的,它有權限 – tabia

相關問題