2016-04-01 130 views
0

我想輸入一些數據到數據庫並上傳圖像到一個特定的目錄。我使用下面的腳本,其頂部的修改版本投票回答了這個問題:How to store file name in database, with other info while uploading image to server using PHP?PHP圖像上傳腳本沒有上傳但沒有失敗

require($_SERVER['DOCUMENT_ROOT']."/settings/functions.php"); 
// This is the directory where images will be saved 
$target = $_SERVER['DOCUMENT_ROOT']."/safetyarticles/images/"; 
$target = $target . basename($_FILES['article_img']['name']); 
date_default_timezone_set("America/Chicago"); 

// This gets all the other information from the form 
$article_name = $_POST['article_title']; 
$article_date = date("m/d/Y"); 
$article_creator = $_POST['article_creator']; 
$article_views = "0"; 
$article_content = $_POST['article_content']; 
$article_content_2 = $_POST['article_content_2']; 
$article_img = ($_FILES['article_img']['name']); 
$article_credit = $_POST['article_credit']; 


// Connect to database 
$conn = getConnected("safetyArticles"); 

// moves the image 
if(move_uploaded_file($_FILES['article_img']['tmp_name'], $target))  
{ 
// if upload is a success query data into db 
mysqli_query($conn, "INSERT INTO currentArticles (article_name, article_date, article_creator, article_content, article_content_2, article_img, article_credit) 
VALUES ('$article_name', '$article_date', '$article_creator', '$article_views', '$article_content', '$article_content_2', '$article_img', '$article_credit')") ; 

echo "The file ". basename($_FILES['article_img']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 
echo "Sorry, there was a problem uploading your file."; 
} 

我已經成功地連接到我的數據庫,因爲我getConnected()函數包含錯誤處理,如果連接失敗。

由於某些原因,我一直從腳本底部收到Sorry, there was a problem uploading your file.錯誤。

我錯過了什麼嗎?我所做的只是改變一些次要的線條,例如數據庫如何連接以及變量。我還將查詢移至只有在文件上傳時才發生。

我不確定我錯過了什麼。

是否也可以修改此當前腳本以將圖像重命名爲$article_name的值?例如,如果文章名稱是「This Is The First Article」,那麼圖像將是this-is-the-first-article.jpg

我的HTML形式是:

<form method="post" action="http://example.com/admin/articleCreate.php" enctype='multipart/form-data'> 
    <input type="text" name="article_title" placeholder="What Is The Name Of This Article?" id="article_title_input"> 
    <textarea name="article_content" placeholder="Write The Top Half Of Your Article Here." id="article_content_input"></textarea> 
    <input type="file" name="article_img" placeholder="If The Article Has An Image, Upload It Here." id="article_img_input"> 
    <textarea name="article_content_2" placeholder="Write The Bottom Half Of Your Article Here." id="article_content_2_input"></textarea> 
    <input type="text" name="article_creator" placeholder="Who Is Writing This Article?" id="article_creator_input"> 
    <input type="text" name="article_credit" placeholder="If This Article Is Copied, What Website Was It Taken From?" id="article_credit_input"> 
    <input type="submit" value="Submit"> 
</form> 

而我做到var_dump(is_uploaded_file($_FILES['article_img']['tmp_name']));和它的returnign true

+0

Is directory/safetyarticles/images/exists並且腳本有權寫入那裏嗎? –

+0

目錄存在,'safetyarticles'和'images'具有'777'權限。 –

+0

將777權限放在任何東西上都不是一個好習慣 –

回答

1

旁註編輯:這之中您之前編輯你的問題,只有被重命名其中的一個。 https://stackoverflow.com/revisions/36367407/4

  • $_FILES['photo']
  • $_FILES['uploadedfile']

是兩個不同的文件陣列和你使用name="article_img"作爲名稱屬性。

你需要爲它們全部使用同一個。

錯誤報告http://php.net/manual/en/function.error-reporting.php會告訴你有關它。

error reporting添加到您的文件的頂部,這將有助於發現錯誤。

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

// Then the rest of your code 

旁註:顯示錯誤應該只在分期完成,並且從不生產。


附加編輯:

$target = $target . basename($_FILES['photo']['name']); 

如果這是你真正的編輯,還是錯誤的數組名。

+0

是的,只是修正了那個。我完全隔開了代碼中的名字與我的名字不同,但仍然不起作用。 –

+0

OP正在對原始帖子進行太多修改。 –

+0

@JesseElser然後添加錯誤報告,正如我在答案中顯示的那樣。如果沒有任何結果,我會刪除我的答案。 –

0

我認爲這個問題是在這一行:

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 

它改成這樣:

if(move_uploaded_file($_FILES['article_img']['tmp_name'], $target))  
+0

沒有變化。 var_dump在文件上返回false。 –

+0

你可以print_r($ _ FILES),所以你可以看到輸入文件是否發佈。 – hayzem

0

你的HTML有:

<input type="file" name="article_img" placeholder="If The Article Has An Image, Upload It Here." id="article_img_input"> 

而且你的PHP正在等待$ _FILES ['photo'] ['tmp_name']

你的HTML文件輸入更改爲:

<input type="file" name="photo" placeholder="If The Article Has An Image, Upload It Here." id="article_img_input"> 
+0

我其實只是改變了這一行的PHP適當的'article_img'名稱,它也是這樣做的。 –

+0

不要忘記這一行$ target = $ target。 basename($ _FILES ['photo'] ['name']); –