上传目录中的图片

时间:2015-03-05 16:28:12

标签: php image upload

我会尝试编写一个用户自己提出问题的脚本。我能够安全地携带标题,描述和其他一些细节。但我想让用户上传图像(也是暂时的),遗憾的是我不能,所以请你帮忙。

粘贴代码:

    <?php require_once 'app/init.php';

if (isset($_POST['submit']))
{
    $validator = Validator::make(
        array(
            'title' => $_POST['title'],
            'question' => $_POST['question'],
        ),
        array(
            'title' => 'required',
            'question' => 'required',
        )
    );

    if ($validator->fails())
    {
        $errors = $validator->messages();
    }
    else
    {
        DB::table('question')->insert(
            array(
                'q_title' => escape($_POST['title']), 
                'q_desc' => escape($_POST['question']),
                'page_title' => escape($_POST['title']), 
                'h_image' => escape($_POST['filename']),
                'user_id' => escape($_POST['userid']),
                'user_name' => escape($_POST['username'])
            )
        );

        return redirect_to('question.php');
    }
}
?>
<?php echo View::make('header')->render() ?>

<div class="row">
    <div class="col-md-8">
        <h3 class="page-header">Question</h3>      
        <!-- Display errors, if are any -->
        <?php if (isset($errors)): ?>
        <ul>
            <?php foreach ($messages->all('<li>:message</li>') as $message) {
                echo $message;
            } ?>
        </ul>
        <?php endif ?>

        <!-- Form -->
        <?php if (Auth::check()): ?>
        <form action="" method="POST" enctype="multipart/form-data">
            <label for="title">Title</label>
        <div class="input-group input-group-lg"> 
            <input type="text" name="title" class="form-control" placeholder="Title" aria-describedby="basic-addon2">
            <span class="input-group-addon" id="basic-addon2">?</span>
            </div><br />
            <label for="question">Description</label>
            <textarea class="form-control" name="question" rows="4" cols="10" placeholder="Description..."></textarea><br />
            <label for="question">Image</label>
            <input type="file" name="filename" id="image" size="40">
            <input type="hidden" name="userid" value="<?php echo Auth::user()->id ?>">
            <input type="hidden" name="username" value="<?php echo Auth::user()->display_name ?>"><br />
        <button type="submit" name="submit" class="btn btn-md btn-success">Help me!</button>
        </form>
        <?php else: ?>
            <p>
            <!-- <?php _e('comments.logged_in', array('attrs' => 'href="login.php"')) ?> -->
            <?php _e('comments.logged_in', array('attrs' => 'href="#" class="login-modal" data-target="#loginModal"')) ?>
            </p>
        <?php endif ?>       
    </div>
</div>

<?php echo View::make('footer')->render() ?>

我希望这些照片都包含在一个目录中(或者如果你告诉我,即使在数据库中,哪个更好?)。

我希望得到你的帮助,谢谢你。

2 个答案:

答案 0 :(得分:0)

首先你应该提交帖子(普通数据和文件数据)

$data = $_POST['data'];
$files = $_FILES['file'];

var_dump $files,您会看到一堆数据,您可以在语句中做很多事情。

然后你应该检查是否确实上传了文件:

(file_exists($_FILES['file']['tmp_name'])) || (is_uploaded_file($_FILES['file']['tmp_name']

如果上传了文件,您可以检查是否存在用于存储文件的现有目录:

$directory = 'your path here';
$filename = $directory.'/'. $_FILES['file']['name'];

if (!file_exists($directory)) {

    mkdir($directory, 0777, true);

}

move_uploaded_file($_FILES['file']['tmp_name'], $filename);

我希望这对你有所帮助...

答案 1 :(得分:0)

简单示例:

HTML

<form action='upload.php' method='post' enctype='multipart/form-data'>
 <input name='filename' type='file' />
 <input name='btnSubmit' type='submit' />
</form>

PHP

<?php
 $filename = $_FILES["filename"]["name"];
 $tmpFilename = $_FILES["filename"]["tmp_name"];
 $path = "path/to/upload/" . $filename;

 if(is_uploaded_file($tmpFilename)){ // check if file is uploaded
  if(move_uploaded_file($tmpFilename, $path)){ // now move the uploaded file to path (directory)
   echo "File uploaded!";
  }
 }
?>

提交表格时, 使用文件信息创建$ _FILES全局变量!

您可以使用此功能在该变量中看到:

<?php
  print_r($_FILES); // this will echo any array
?>