将图像发送到用户目录

时间:2015-03-25 18:28:59

标签: php upload dirname

我正在尝试创建一个网站,用户可以在其中创建自己的帐户并在我的主机中自动创建他的文件夹 我写这个:

$addtothedb="INSERT INTO login(firstname,lastname,useremail,password,Gender,Dateofbirth) VALUES ('". $fristname . "','". $laname ."','" . $email . "','". $pass ."')" ;          
$result=mysqli_query($con,$addtothedb);
$cur = "USERS/";
$gofile=$cur.$email;
if($result){
   if ( mkdir($gofile,0777) ) {
     } else {
      }
   echo "  Account Successfully Created. <br> Now you can Login. ";                       
  } 
  else {
   echo "<center>Failure</center>";
}

现在他上传了他的照片。我希望照片转到他的文件夹而不是USERS / Directory。 我尝试这个脚本但不起作用,我想要的。

<?php
     $con = mysqli_connect("localhost","root","","test");
     echo $_SESSION['name']."<br />" ;  
    $cur = "USERS/";
    $gofile=$cur .$_SESSION["name"];
    echo $gofile;

    $des= dirname($gofile.'/'.$_SESSION['name']);echo "<br/>";  
    echo $des;
?>

2 个答案:

答案 0 :(得分:1)

你的变量$ gofile并没有像你想象的那样构建。输出将是这样的:

USERS/pokeybit/pokeybit

$ des应该是:

$des=$cur."/".$_SESSION['name']; //for the path to be "USERS/pokeybit"
$des=$_SESSION['name']; //for the path to be "pokeybit"
$des="../".$_SESSION['name']; //for the path to be "parent_folder/pokeybit"

答案 1 :(得分:0)

您的脚本无法上传文件并将其放入目录。首先,你需要一个表格,我有一个基本的上传脚本,让你去。

XHTML表格

<form action="accept-file.php" method="post" enctype="multipart/form-data">
    Your Photo: <input type="file" name="photo" size="25" />
    <input type="submit" name="submit" value="Submit" />
</form>

您需要为表单的enctype属性使用multipart / form-data值。您显然还需要至少一个文件类型的输入元素。表单的操作标记必须提供一个URL,该URL指向包含以下PHP部分的文件。 PHP

//if they DID upload a file...
if($_FILES['photo']['name'])
{
    //if no errors...
    if(!$_FILES['photo']['error'])
    {
        //now is the time to modify the future file name and validate the file
        $new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
        if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
        {
            $valid_file = false;
            $message = 'Oops!  Your file\'s size is to large.';
        }

        //if the file has passed the test
        if($valid_file)
        {
            //move it to where we want it to be
            move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
            $message = 'Congratulations!  Your file was accepted.';
        }
    }
    //if there is an error...
    else
    {
        //set that to be the returned message
        $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
    }
}

//you get the following information for each file:
$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']