在这个PHP代码中,我想自定义图像上传目的地。有了这个php文件,我有一个名为uploads的目录。我想将我上传的所有图像添加到此目录并在db中存储路径。我怎么能这样做?
<?php
// Assigning value about your server to variables for database connection
$hostname_connect= "localhost";
$database_connect= "image_upload";
$username_connect= "root";
$password_connect= "";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
@mysql_select_db($database_connect) or die (mysql_error());
if($_POST) {
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
if ($_FILES["file"]["error"] > 0) {
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
//move_uploaded_file function will upload your image. if you want to resize image before uploading see this link http://b2atutorials.blogspot.com/2013/06/how-to-upload-and-resize-image-for.html
if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"])) {
// If file has uploaded successfully, store its name in data base
$query_image = "insert into acc_images (image, status, acc_id) values ('".$_FILES['file']['name']."', 'display','')";
if(mysql_query($query_image)) {
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
} else {
echo 'File name not stored in database';
}
}
}
}
}
?>
目前我运行上传时 我收到了警告
警告:move_uploaded_file(images / 1409261668002.png):无法打开流:第29行的D:\ xampp \ htdocs \ image-upload \ index.php中没有此类文件或目录
警告:move_uploaded_file():无法在第29行的D:\ xampp \ htdocs \ image-upload \ index.php中将'D:\ xampp \ tmp \ php1C1F.tmp'移动到'images / 1409261668002.png'
答案 0 :(得分:0)
您必须指定正确的路径,如果您不创建'images/1409261668002.png'
路径并且未指定它们,则if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]))) { .... }
路径不存在。
SELECT SUBSTRING(Username, CHARINDEX('/', Username) + 1, LEN(Username)), UserType
FROM USERS_DATABASE;
您必须指定绝对路径
答案 1 :(得分:0)
您可以使用以下代码:
$image=basename($_FILES['file']['name']);
$image=str_replace(' ','|',$image);
$tmppath="images/".$image;
if(move_uploaded_file($_FILES['file']['tmp_name'],$tmppath))
{...}
如果您对此有任何疑问/疑虑,请与我们联系。