我无法将文件上传到上传文件夹。我没有使用公用文件夹,我删除了公用文件,并在根目录中移动了我的文件夹结构。
app
bootstrap
config
css
database
fonts
images
storage
tests
vendor
uploads
.env
.htaccess
index
我的AashishController.php
<?php
namespace App'Http'Controllers;
use Input;
use Mail;
class AashishController extends Controller
{
public function index()
{
return view('in');
}
public function contact()
{
return view('contact');
}
public function postcontact()
{
$name = 'Input::get('name');
$email = 'Input::get('email');
$phone = 'Input::get('phone');
$message1 = 'Input::get('message');
$file1 = Input::file('file1');
$userr=$file1;
//Create directory if it does not exist
if(!is_dir("uploads/".$userr."/")) {
mkdir("uploads/".$userr."/");
}
// Move the uploaded file
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/".$userr."/". $_FILES["file"]["name"]);
$to ="email@example.com";
$subject = "hi";
$mail_body = '<html>
<body bgcolor="#573A28" topmargin="25">
Put HTML content here with variables from PHP if you like
Variable display Example:
</body>
</html>';
$headers = "From:email@example.com";
$headers .= "Content-type: text/html";
mail($to, $subject, $mail_body, $headers);
return view('test')->with('name', $name);
}
}
如何从该目录中获取文件并将其传递给视图?
您有任何错误吗?
您可能忘记了使用chmod 777对上传文件夹授予写入权限。
edit:从mkdir路径中删除最后一个斜杠。
并确保$userr
变量包含具有dd($userr);
的有效值
哦,好吧,我现在明白了。
1-您试图发送一个对象,而不是向mkdir函数发送一个字符串。
2-即使我假设你正试图将文件名发送到mkdir,为什么你需要创建一个用该文件名命名的文件夹?只需使用时间戳或一些类似于rand(100999)的随机值来创建子文件夹。
3-你的$userr包含这样一个对象:
object(Symfony'Component'HttpFoundation'File'UploadedFile)[9]
private 'test' => boolean false
private 'originalName' => string 'test.jpg' (length=22)
private 'mimeType' => string 'image/jpeg' (length=10)
private 'size' => int 667220
private 'error' => int 0
因此,将它用作对象甚至声明它都没有意义。只使用Input::file('file1')。
public function postcontact()
{
$name = 'Input::get('name');
$email = 'Input::get('email');
$phone = 'Input::get('phone');
$message1 = 'Input::get('message');
$file1 = Input::file('file1');
$timestamp = time();
//Create directory if it does not exist
if(!is_dir("uploads/".$timestamp)) {
mkdir("uploads/".$timestamp);
}
$sitepath = $_SERVER['DOCUMENT_ROOT'];
// Move the uploaded file
Input::file('file1')->move($sitepath.'/uploads/'.$timestamp.'/', Input::file('file1')->getClientOriginalName());
$to ="email@example.com";
$subject = "hi";
$mail_body = '<html>
<body bgcolor="#573A28" topmargin="25">
Put HTML content here with variables from PHP if you like
Variable display Example:
</body>
</html>';
$headers = "From:email@example.com";
$headers .= "Content-type: text/html";
mail($to, $subject, $mail_body, $headers);
return view('test')->with('name', $name);
}
对于访问您视图中文件夹的路径,我总是更喜欢使用这种风格:
$data['name'] = $name;
$data['filepath'] = "uploads/".$timestamp."/".Input::file('file1')->getClientOriginalName();
return view('test',$data);
在您的视图中,您可以使用刀片模板引擎访问这些变量::
name {{ $name }} <br />
filepath {{ $filepath }} <br />
或者通过使用原始php变量:
name <?php echo $name; ?> <br />
filepath <?php echo $filepath; ?> <br />
我想你没有用表单发送文件,你的表单应该是:
{{ Form::open(["url"=>"/someurl", "files"=>true,])}
粘贴正在发布数据的视图
在将文件移到上传文件夹之前,您应该检查文件类型。
而不是
$sitepath = $_SERVER['DOCUMENT_ROOT'];
使用
$sitepath = public_path();