2012-05-10 37 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