上传目录中的图片
Upload image in directory
我会尝试编写一个脚本,供用户提出自己的问题。我能够安全地携带标题、描述和其他一些详细信息等字段。但是我想让用户上传图片(也是暂时的),可惜我不能,所以我请求帮助你。
粘贴代码:
<?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() ?>
我希望这些图片包含在一个目录中(或者如果你告诉我,即使在数据库中,哪个更好?)。
希望得到您的帮助,谢谢。
首先你应该提交post(普通数据和文件数据)
$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);
希望对您有所帮助...
简单示例:
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
?>
我会尝试编写一个脚本,供用户提出自己的问题。我能够安全地携带标题、描述和其他一些详细信息等字段。但是我想让用户上传图片(也是暂时的),可惜我不能,所以我请求帮助你。
粘贴代码:
<?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() ?>
我希望这些图片包含在一个目录中(或者如果你告诉我,即使在数据库中,哪个更好?)。
希望得到您的帮助,谢谢。
首先你应该提交post(普通数据和文件数据)
$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);
希望对您有所帮助...
简单示例:
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
?>