2013-03-14 547 views
0

我的腳本有問題。它在我的託管服務器上傳圖像。在第二天或下週,主機服務器中沒有創建帶有圖像的文件夾,當然不是所有圖像都是 - 2周內的全部圖像的18-19%。我問他們。他們告訴我 - 問題出在我的php代碼中。如果有人能幫助,我會很高興。可能是我在mkdir()中添加了一些0777?我不知道。我一直在想...php圖像上傳mysql數據庫

<?php 

if (isset($_FILES['myfile'])) { 
    $ip = getRealIpAddr(); 
    $usernameupload = $user_data['username']; 

    $title = sanitize($_POST['title']); 
    $description = sanitize($_POST['description']); 
    if (empty($_POST['folder']) === true) { $folder = sanitize($_POST['folder_option']);} else { $folder = sanitize($_POST['folder']);} 
    $date = date('M-d-Y'); 
    $code = ""; 

    $errors = array(); 
    $allowed_ext =array('jpg', 'jpeg', 'png', 'gif'); 

    $file_name = $_FILES['myfile']['name']; 
    $file_size = $_FILES['myfile']['size']; 
    $file_tmp = $_FILES['myfile']['tmp_name']; 
    $extension=explode('.',$file_name); 
    $file_ext=strtolower(end($extension)); 

    if (isset($_POST['submitupload'])) { 
     if ($_FILES['myfile']['name'] == ""){ 
     $errors[] = ' *Upload file'; 
     } else if (in_array($file_ext, $allowed_ext) === false) { 
     $errors[] = ' *Extension not allowed'; 
     } 
    } 

    if ($file_size > 5042880) { 
     $errors[] = 'File size must be under 5mb'; 
     } 
    if (strlen($folder) > 50) { 
     $errors[] = 'Your new folder name must be max 50 characters'; 
     } 

    if (empty($_POST['title'])) { 
     $errors[] = 'Please Fill Title'; 
     } 

    if (strlen($_POST['title']) > 255) { 
     $errors[] = 'Your title must be max 255 characters'; 
     } 

    if (empty($errors)) { 

     require('db.inc.php'); 

     $charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
     $length = 50; 

     for ($i = 0; $i <= $length; $i++) { 
      $rand = rand() % strlen($charset); 
      $tmp = substr($charset, $rand, 1); 
      $code .= $tmp; 
      } 

     $query = mysql_query("SELECT code FROM files WHERE code='$code'"); 
     $numrows = mysql_num_rows($query); 

     while ($numrows != 0) { 
       for ($i = 0; $i <= $length; $i++) { 
       $rand = rand() % strlen($charset); 
       $tmp = substr($charset, $rand, 1); 
       $code .= $tmp; 
       } 
     $query = mysql_query("SELECT code FROM files WHERE code='$code'"); 
     $numrows = mysql_num_rows($query); 
     } 

     mkdir("files/$code"); 
     mkdir("files/thumbs/$code"); 


     if (move_uploaded_file($file_tmp, "files/$code/".$file_name)) { 

     $file = "";  
     $query = mysql_query("INSERT INTO files VALUES ('', '$usernameupload', '$ip', '$title', '$code', '$folder', '$description', '$file_name', '$file_ext', '$file_size', '$date')"); 

     $query1 = mysql_query("INSERT INTO votes VALUES ('', '', '', '$code')"); 

     create_thumb('files/'.$code.'/', $file_name, 'files/thumbs/' . $code . '/'); 


     header("Location: userdownload.php?code=$code"); 
     exit(); 

      } 
     } 
    } 
?> 

<form action="" method="post" enctype="multipart/form-data"> 
    <table> 
     <tr> 
      <td id="editfill">Image:*</td> 
      <td><input type="file" name="myfile" id="fileup"></td> 
      <tr> 
      <td></td> 
      <td><?php 

      if (isset($_POST['submitupload'])) { 
     if ($_FILES['myfile']['name'] == ""){ 
     echo '<div id="errormsg"> *Upload file </div>'; 
     } else if (in_array($file_ext, $allowed_ext) === false) { 
     echo '<div id="errormsg"> *Allowed extension: .jpg, .jpeg, .png, .gif</div>'; 
     } else if ($file_size > 5042880) { 
     echo '<div id="errormsg"> *File size must be under 5mb </div>'; 
     } 
    } 
    ?></td> 
    </tr> 
     </tr> 
     <tr> 
      <td id="editfill">Title:*</td> 
      <td><input type="text" name="title" id="title" value="<?php if (isset($_POST['submitupload'])) echo htmlentities($_POST['title']); ?>" maxlength="255"></td> 
     <tr> 
     <td></td> 
      <td><?php 
      if (isset($_POST['submitupload'])) { 
      if (empty($_POST['title'])) { 
     echo '<div id="errormsg"> *Please fill title </div>'; 
     } 
      if (strlen($_POST['title']) > 255) { 
     echo '<div id="errormsg"> *Your title must be max 255 characters</div>'; 
     } 
     } 
      ?></td> 
     </tr> 
     </tr> 
     <tr> 
      <td id="editfill">Create Album:</td> 
      <td><input type="text" name="folder" id="title" value="<?php if (isset($_POST['submitupload'])) echo htmlentities($_POST['folder']); ?>" maxlength="50"> 
      </td> 
     <tr> 
     <td></td> 
     <td><?php 
     if (strlen($folder) > 50) { 
     echo '<div id="errormsg"> *Your new album name must be max 50 characters</div>'; 
     } 
?></td> 
     </tr>  
     </tr> 
     <tr> 
      <td id="editfill">Your Albums:</td> 
      <td> 
      <select name="folder_option" class="select"> 
      <option></option> 
     <?php 
     $mysql_folder = mysql_query("SELECT `folder_name` FROM `files` WHERE `username` = '$usernameupload' AND `folder_name` > '' GROUP BY `folder_name` ORDER BY `folder_name` ASC"); 

     while ($query_row = mysql_fetch_array($mysql_folder)) { 
       $filefolder = $query_row['folder_name']; 

       echo '<option value="' . $filefolder . '">' . $filefolder . '</option>'; 
       } 

     ?> 
    </select> 
      </td> 
     <tr> 
     <td></td> 
     <td></td> 
     </tr>  
     </tr> 
     <tr> 
      <td id="editfill">Description:</td> 
      <td><textarea name="description" id="description"><?php if (isset($_POST['submitupload'])) echo htmlentities($_POST['description']); ?></textarea></td> 
     <tr><td></td> 
      <td> 
     </td> 
     </tr>  
     </tr> 
     <tr> 
      <td></td> 
      <td><center><br><input type="submit" name="submitupload" id="submitupload" value="Upload"></center></td> 
     </tr> 
    </table> 
</form> 
+0

你是說你從過去2週上傳的一些照片不見了? – 2013-03-14 07:19:14

+0

Еxactly。他們缺少創建的文件夾和圖像裏面。 – user1929805 2013-03-14 07:23:24

+1

你沒有在這種情況下定義任何文件刪除,這是非常不可能的代碼刪除創建的文件..除了當你試圖創建文件夾,然後插入圖像..也許它出現了一個錯誤,同時創建這就是爲什麼它停止,文件夾和圖像不會被創建,但仍然插入數據庫.. – 2013-03-14 07:29:21

回答

1

試試這個有效地看看如果mkdir創建沒有失敗。 其他文件夾和圖像肯定不會存在,即使它被添加到您的數據庫。

//previous code... 
mkdir("files/$code"); 
mkdir("files/thumbs/$code"); 

//change to this... 
if (!mkdir("files/$code", 0777, true)) { 
    die('Failed to create folders...'); 
} 

if (!mkdir("files/thumbs/$code", 0777, true)) { 
    die('Failed to create sub folders...'); 
} 
+0

已上傳的圖片和上傳沒有問題 – user1929805 2013-03-14 08:07:56

+0

感謝您的支持。是否有可能我的問題將從0777,真實? – user1929805 2013-03-14 08:09:04

+0

我現在必須等待結果。 – user1929805 2013-03-14 08:11:25