2013-05-18 33 views
0

我試着上傳2圖像,但它似乎一切都很好,但它move_uploaded_file()不移動該文件,沒有錯誤已生成。功能不移動圖像,但正確返回信息

// Upload Image 
function upload_image($tmp,$name) { 
    $ext = explode('.',$name); 
    if (preg_match('~(jpeg|jpg|gif|png)$~i', $name)) { 
    $ext = end($ext); 

    $name = substr(md5(rand()), 0, 10).'.' . $ext; 
    $upload_dir = '/music/uploads/' . $name; 
    echo $tmp; 
    move_uploaded_file($tmp,'/home/shadne/public_html'.$upload_dir); 

     return $upload_dir; 
    } else { 
     throw new ErrorException('File type not allowed'); 
    } 
} 

文件/文件夾的權限是0777,我用圖片/新使用前/上傳

編輯1

代碼來處理上傳和籤

<?php 
require_once '../config.php'; 
require 'includes/functions.php'; 

if (!empty($_FILES['artist_profile_image'])) { 
    $profile_image_name = $_FILES['artist_profile_image']['name']; 
    $profile_image_tmp = $_FILES['artist_profile_image']['tmp']; 
    $profile_image_dir = upload_image($profile_image_tmp, $profile_image_name); 
} 

if (!empty($_FILES['artist_thumb'])) { 
    $thumb_name = $_FILES['artist_thumb']['name']; 
    $thumb_tmp = $_FILES['artist_thumb']['tmp']; 
    $thumb_dir = upload_image($thumb_tmp, $thumb_name); 
} 

if (empty($_POST) === false) { 
    $required_fields = array('artist_name', 'artist_thumb', 'artist_profile_image', 'birthday'); 
    foreach ($_POST as $key => $value) { 
      if (empty($value) && in_array($key, $required_fields) === true) { 
       $errors[] = 'Fields marked with an asterstrik are required'; 
       break 1; 
      } 
    } 
} 

if (isset($_GET['success'])) { 
    echo 'You\'ve added a new artist.'; 
} else { 
    if (empty($_POST) === false && empty($errors) === true) { 
     $artist_data = array(
      'artist_name'   => $_POST['artist_name'], 
      'artist_thumb'   => $thumb_dir, 
      'artist_profile_image' => $profile_image_dir, 
      'birthday'    => $_POST['birthday'], 
     ); 

     add_artist($artist_data); 
     //exit(header('Location: add_artist.php?success')); 

    } else if (empty($errors) === false) { 
     echo output_errors($errors); 
    } 
?> 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8" /> 
     <title>Admin Area</title> 
    </head> 
    <body> 

<h1>Add Artist</h1> 

<form action="" method="post" enctype="multipart/form-data"> 
    <ul> 
     <li> 
      Artist Name*: <input type="text" name="artist_name" /> 
     </li> 
     <li> 
      Artist Thumbnail*: <input type="file" name="artist_thumb" /> 
     </li> 
     <li> 
      Artist Profile Image*: <input type="file" name="artist_profile_image" /> 
     </li> 
     <li> 
      Birthday*: 
        <input type="date" name="birthday" /> 
     </li>      
     <li> 
     <input type="submit" value="Add Artist" /> 
     </li> 
    </ul> 
</form> 
<?php } ?> 
    </body> 
</html> 
+0

是'echo $ tmp;'輸出什麼東西?是否達到了代碼的一部分? – pseudoh

+0

你能否啓用錯誤報告..你的腳本例子有很多錯誤'substr(md5(rand()),0,10)。'。' 。 $ ext;'當'$ ext'是'array' ... – Baba

+0

@pseudoh不是它不是 –

回答

1

您需要檢查move_uploaded_file是否返回true或false。如果輸入文件不是有效的上傳文件,它可以靜默失敗。 你將什麼發送到$ tmp?

檢出:http://php.net/manual/en/function.move-uploaded-file.php返回值。

+0

它返回false,但我不知道爲什麼 –

+0

根據PHP文檔,臨時文件不是[「tmp」],而是[「tmp_name」],請嘗試在代碼中替換該文件,並確認您確實正在獲取從它的文件路徑。 http://php.net/manual/en/reserved.variables.files.php – Wildex999

+0

謝謝你,似乎我專注於錯誤的代碼。 –

相關問題