2012-05-10 72 views
-1

我想了一個WordPress的模板中下面的代碼:上傳PHP代碼沒有上傳圖像

<?php 
//connect to database. Username and password need to be changed 
mysql_connect("localhost", "username", "password"); 

//Select database, database_name needs to be changed 
mysql_select_db("database_name"); 

if (!$_POST['uploaded']){ 
//If nothing has been uploaded display the form 
?> 

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" 
ENCTYPE="multipart/form-data"> 
Upload:<br><br> 
<input type="file" name="image"><br><br> 
<input type="hidden" name="uploaded" value="1"> 
<input type="submit" value="Upload"> 
</form> 

<? 
}else{ 
//if the form hasn't been submitted then: 

//from here onwards, we are copying the file to the directory you made earlier, so it can then be moved 
//into the database. The image is named after the persons IP address until it gets moved into the database 

//get users IP 
$ip=$REMOTE_ADDR; 

//don't continue if an image hasn't been uploaded 
if (!empty($image)){ 

//copy the image to directory 

copy($image, "/temporary/".$ip.""); 

//open the copied image, ready to encode into text to go into the database 
$filename1 = "/temporary/".$REMOTE_ADDR; 
$fp1 = fopen($filename1, "r"); 

//record the image contents into a variable 
$contents1 = fread($fp1, filesize($filename1)); 

//close the file 
fclose($fp1); 

//encode the image into text 
$encoded = chunk_split(base64_encode($contents1)); 

//insert information into the database 
mysql_query("INSERT INTO images (img,data)"."VALUES ('NULL', '$encoded')"); 

//delete the temporary file we made 
unlink($filename1); 
} 

//end 
} 
?> 

我創建了一個在我的網站根所謂的「臨時」目錄。該文件位於:wp-content/themes/mytheme /目錄中。

我還設置目錄權限和chmod 777

當我嘗試上傳什麼也沒有發生,沒有什麼被上傳到目錄中,並沒有什麼添加到數據庫中無論是。

我嘗試上傳後發生的一切是頁面重定向到主頁。

+0

試試這個http://www.tizag.com/phpT/fileupload.php –

+0

有沒有MySQL數據庫參與該代碼 – Satch3000

+0

你不上傳文件到數據庫。您只能將file_path保存到數據庫。 –

回答

1

這是什麼?

if (!empty($image)){ 

您的圖片是在陣列

$_FILES['image'] 
+0

不明白你的解決方案是...你能解釋一下嗎? – Satch3000

+0

http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx僱用您可以找到有關$ _FILES的信息並將其存儲到數據庫 – YamahaSY