2015-10-19 55 views
1

我創建了一個測試頁面,瞭解如何上傳圖片。我試圖將圖像保存到我創建的名爲image的文件夾中,然後在我的數據庫中存儲該圖像的文件名,以幫助處理空間。現在圖像文件名不存儲,而是字Array正在存儲。我確實遇到了錯誤,以及其他錯誤。我點擊上傳後,我收到以下錯誤:困難創造圖片上傳選項

Warning: move_uploaded_file(image/picturetest.jpg): failed to open stream: No such file or directory in /home4/fdfsfs/public_html/example.com/img_test.php on line 34

Warning: move_uploaded_file(): Unable to move '/tmp/phpUg7p4D' to 'image/picturetest.jpg' in /home4/fdsfafa/public_html/example.com/img_test.php on line 34

There was an error! 

**Notice: Array to string conversion in /home4/fdsfaf/public_html/example.com/img_test.php on line 59

34號線:

if (move_uploaded_file($tmp_name, $destinationFolder.$filename)) { 

59號線:

$stmt->execute(); 

完整的腳本:

$filename = $_FILES['file']['name']; 
//$filesize = $_FILES['file']['size']; 
//$filetype = $_FILES['file']['type']; 
$tmp_name = $_FILES['file']['tmp_name']; 
$file_error = $_FILES['file']['error']; 

if(isset($_POST['create'])){ 
    $file = $filename; 
    $file = $_FILES['file']; 
    //$file = "productpics/". $_FILES['file']['name']; // save the filename 
}else { 
    echo "error!"; 
    } 

if (isset($filename)) { 
    if (!empty($filename)) { 

     $destinationFolder = 'image/'; 

     if (move_uploaded_file($tmp_name, $destinationFolder.$filename)) { 
      echo 'Uploaded!'; 
     } else { 
      echo 'There was an error!'; 
     } 

    } else { 
     echo 'Please choose a file.'; 
    } 
} 


//Connection 
$con = mysqli_connect("localhost","","",""); 
    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 

    if ($stmt = mysqli_prepare($con, "INSERT INTO image (img) VALUES (?)")) { 

        /* bind parameters for markers */ 
        $stmt->bind_param('s', $file); 

        /* execute query */ 
        $stmt->execute(); 
        //if(!$stmt->execute()){trigger_error("there was an error....".$con->error, E_USER_WARNING);} 

       /* close statement */ 
       mysqli_stmt_close($stmt); 
        echo "Success!"; 
       } else { 
        echo "Failed!"; 
       } 

$result = mysqli_query($con,"SELECT * FROM image"); 
    if($row = mysqli_fetch_array($result)) { 
     if($row['img'] == ""){ 
      echo "<img src='images/default_pic.png' alt='No Picture'>"; 
     } else { 
      echo "<img src='images/".$row['img']."' alt='Profile Picture'>"; 
     } 
      echo "<br><br><br><br>"; 
    } 
?> 


<form action="" method="POST" enctype="multipart/form-data"> 
    <input type="file" name="file" class="inputbarfile"> 
    <input type="submit" name="create" id="signinButton" value="Upload"> 
</form> 

有誰看到我在做什麼錯這個???

+0

在腳本的最頂部你寫的'$文件= $ _FILES [ '文件'];'這將是爲什麼你正在寫'Array'到數據庫這裏'$ stmt-> bind_param(「S」,$文件);' – Rasclatt

+1

你會想這可能保存到數據庫:'$ destinationFolder $ filename' – Rasclatt

+0

@Rasclatt我很困惑,你。正在說要做。 – Becky

回答

1

所以要排序的「重做」你有什麼,我已經打破它分解成零部件(功能 )。它基本上是相同的,但是每個部分都是細分的,所以它更容易管理每個部分2)更容易排除故障3)更容易添加錯誤處理。

<?php 
// If you make a file function, you can change where things are saved 
// You can also change the destination (for portability) 
function UploadFile($fileArray = array(), $destinationFolder = 'image/') 
    { 
     $filename  = $fileArray['file']['name']; 
     $tmp_name  = $fileArray['file']['tmp_name']; 
     $filesize  = $fileArray['file']['size']; 
     $file_error  = $fileArray['file']['error']; 
     $file   = $fileArray['file']; 
     // Save all the default data. 
     // Success and error should be set by default to fail 
     $return['error']  = true; 
     $return['success']  = false; 
     $return['file']['dest'] = $destinationFolder.$filename; 
     $return['file']['size'] = $filesize; 

     if($file_error == 0) 
      $return['error'] = false; 
     // I added a directory creation function so you don't have to 
     // manually make folders. This will do it for you. 
     if(!is_dir($destinationFolder)) 
      mkdir($destinationFolder,0755,true); 
     // If your filename is not empty, return success or fail of upload 
     if (!empty($filename)) 
      $return['success'] = (move_uploaded_file($tmp_name, $destinationFolder.$filename)); 

     return $return; 
    } 

// Create a function that quickly returns your connection 
function Connection() 
    { 
     //Connection 
     $con = mysqli_connect("localhost","","",""); 
      if (mysqli_connect_errno()) { 
       printf("Connect failed: %s\n", mysqli_connect_error()); 
       exit(); 
      } 

     return $con; 
    } 

// Create a save-to-database function so it's easier and reusable 
function SaveToDb($con,$filename = false) 
    { 
     // Return fail immediately if the connection is false 
     // or the image name is invalid 
     if(empty($filename) || !$con) 
      return false; 

     if ($stmt = mysqli_prepare($con, "INSERT INTO image (img) VALUES (?)")) { 
       $stmt->bind_param('s', $filename); 
       $stmt->execute(); 
       mysqli_stmt_close($stmt); 

       return true; 
      } 

     return false; 
    } 

// This just gets the image from the destination column. Not the most 
// efficient, but you can change it to fetch by id or whatever is unique 
function getPhoto($con,$dest) 
    { 
     $result = mysqli_query($con,"SELECT * FROM `image` where `img` = '$dest'"); 
     if($row = mysqli_fetch_array($result)) 
      return $row; 

     return 0; 
    } 

使用方法:

// Make sure all functions above are include here 

// Get the database connection 
$con  = Connection(); 
// Check for post 
if(isset($_POST['create'])) { 
     // Try uploading 
     $upload = UploadFile($_FILES); 
     // If upload fails 
     if(!$upload['success']) 
      echo '<h3>Sorry, an error occurred</h3>'; 
     else { 
       // You could add error handling here based on the results of 
       // each function's success or failure below. 

       // Try to save it 
       $saveToDb = SaveToDb($con,$upload['file']['dest']); 
       // Get the profile from image name 
       $profPic = ($saveToDb)? getPhoto($con,$upload['file']['dest']) : false; ?> 

       <img src="<?php echo (!empty($profPic) && $profPic != 0)? $profPic['img'] : "default_pic.png"; ?>" alt="<?php echo (!empty($profPic) && $profPic != 0)? "Profile Picture" : "No Picture"; ?>" /> 
       <?php 
      } 
    } 
?> 
+0

首先,非常感謝您在這樣的細節中進行擴展!好吧,所以這消除了我明顯得到的消息。這絕對超過了我的技能水平,因此我試圖將其全部吸收。每當我上傳照片時,它都會在頁面上以非常大的版本顯示圖像,因此它的實際大小。無論如何,要讓圖片只出現在「您的圖片」框中?如果你願意,你可以看到它在我的網站上做了什麼。我現在只是在玩這個,所以如果有幫助的話,隨意上傳一個圖片。 realtorcatch.com/test_test_img – Becky

+0

是的,您可以使用CSS - >'max-width'和/或'max-height'使它以正確的大小顯示在某個圖像框中。爲了使它顯示在您想要的框中,只需更改邏輯並將'「alt =」<?php echo(!empty($ profPic)&& $ profPic!= 0)? 「個人資料圖片」:「無圖片」; if> – Rasclatt

+0

想要將用戶的信息保存到會話中以填充該字段 – Rasclatt

0

你需要把數組轉換成字符串,看看這個...

http://www.w3schools.com/php/func_string_implode.asp

+0

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 –