我使用php制作了一个小图像上传系统它工作正常但是现在当我尝试在特定文件夹中上传图片时,它只是在根文件夹中提供错误和上传图像。
php代码是:
<?php
//checking if the user already has a directory or not.
if($r['directory']==0){
$query= "UPDATE user SET directory='1' where id='".$_SESSION['sess_id']."'";
mysqli_query($dbconn,$query);
$path='../akshay/work/'.$r['username'];
mkdir($path) or die("unable to create folder");
} if(isset($_POST['submit'])&& $r['directory']==1){
//uploading users work.
$path='../akshay/work/'.$r['username'];
$name=$_FILES['work']['name'];
$size=$_FILES['work']['size'];
$type=$_FILES['work']['type'];
$tmp_name=$_FILES['work']['tmp_name'];
$extension=strtolower(substr($name,strpos($name,'.') + 1));
//checking if the user has selected a file or not.
if(isset($name)){
if(!empty($name)){
//checking if the uploaded file is an image or not.
if($extension=='jpg'||$extension=='jpeg'&& $type=='image/jpeg'){
$location=$path;
if(move_uploaded_file($tmp_name,$location.$name)){
echo'uploaded succesfully';
}else{
echo'<p style=color:red;>please choose a file</p>';
}
} else {
echo'<p style=color:red;>format of the file must be jpg/jpeg</p>';
}
}
}
}
?>
我认为问题出在move_uploaded_file()函数中。 PLZ帮助我解决这个问题,请告诉我任何其他方式(如果有的话)这样做。我能够使用第一次检查创建目录。
错误是&#34;未定义变量:dir_name&#34;并打印按摩&#34;成功上传&#34;。
我已在代码中进行了一些编辑,现在我可以在指定的位置创建一个目录,但无法上传该文件夹中的图像,而是将图像上传到“工作”中。文件夹和用户名附加到图像名称。
请帮助!!!
答案 0 :(得分:1)
在代码的else if
部分中,未定义$ dir_name。
因此,请更新您的代码并添加以下内容:
else if(isset($_POST['submit'])&& $r['directory']==1){
$dir_name = $r['username'];
// your code continue..
此外..您使用的代码并不好。好像if
条件为真,那么else if
部分将不会被执行。所以更新你的代码如下:
<?php
//checking if the user already has a directory or not.
if($r['directory']==0){
$query= "UPDATE user SET directory='1' where id='".$_SESSION['sess_id']."'";
mysqli_query($dbconn,$query);
$dir_name=$r['username'];
$r['directory']=1;
mkdir($dir_name);
}
if(isset($_POST['submit'])&& $r['directory']==1){
$dir_name = $r['username'];
//uploading users work.
$name=$_FILES['work']['name'];
$size=$_FILES['work']['size'];
$type=$_FILES['work']['type'];
$tmp_name=$_FILES['work']['tmp_name'];
$extension=strtolower(substr($name,strpos($name,'.') + 1));
//checking if the user has selected a file or not.
if(isset($name)){
if(!empty($name)){
//checking if the uploaded file is an image or not.
if($extension=='jpg'||$extension=='jpeg'&& $type=='image/jpeg'){
if(move_uploaded_file($tmp_name,$dir_name.$name)){
echo'uploaded succesfully';
}else{
echo'<p style=color:red;>please choose a file</p>';
}
}
else
{
echo'<p style=color:red;>format of the file must be jpg/jpeg</p>';
}
}
}
}
?>
答案 1 :(得分:0)
使用此代码..
$directory ="../images/".$file_name;
move_uploaded_file($temp_name, $directory);
我认为这对你有用