简体   繁体   English

上传目录中的图片

[英]Upload image in directory

I would try to write a script where users make their own questions. 我会尝试编写一个用户自己提出问题的脚本。 I was able to safely carry the fields such as title, description and some other details. 我能够安全地携带标题,描述和其他一些细节。 But I want to make users upload images (also for the moment), unfortunately I can not, I ask therefore help to you. 但我想让用户上传图片(也是暂时的),遗憾的是我不能,所以请你帮忙。

Paste the code: 粘贴代码:

    <?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() ?>

I wish the pictures were included in a directory (or if you advise me, even in the database, what is better?). 我希望这些图片都包含在一个目录中(或者如果你告诉我,即使在数据库中,哪个更好?)。

I hope for your help, thank you. 希望对你有所帮助,谢谢。

First you should submit the post (normal data & file data) 首先你应该提交帖子(普通数据和文件数据)

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

var_dump the $files , and you will see a bunch of data, where you can do a lot with in statements. var_dump $files ,你会看到一堆数据,你可以用in语句做很多事情。

Then you should check if there is actually a file uploaded: 然后你应该检查是否确实上传了一个文件:

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

If a file is uploaded, you could check if there is an existing directory to store the files in: 如果上传了文件,您可以检查是否存在用于存储文件的现有目录:

$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);

I hope this helped you a little... 我希望这对你有所帮助......

Simple Example: 简单示例:

HTML HTML

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

PHP 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!";
  }
 }
?>

When you submit the form, the $_FILES global variable is created with the infomation on the file! 提交表单时,会在文件信息中创建$ _FILES全局变量!

You can see that's in that variable with this function : 您可以使用此函数在该变量中看到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM