我正在尝试从现有文件创建UploadedFile。但它没有成功上传,也没有显示任何错误。
这是我的代码
[ErrorException]
Argument 2 passed to Illuminate\Database\Eloquent\Factory::{closure}() must be an instance of Faker\Provider\it_IT\PhoneNumber, array given
Exception trace:
() at /home/vagrant/php/housing/database/factories/ModelFactory.php:19
Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at /home/vagrant/php/housing/database/factories/ModelFactory.php:19
Illuminate\Database\Eloquent\Factory::{closure}() at n/a:n/a
call_user_func() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:130
Illuminate\Database\Eloquent\FactoryBuilder->Illuminate\Database\Eloquent\{closure}() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2308
Illuminate\Database\Eloquent\Model::unguarded() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:133
Illuminate\Database\Eloquent\FactoryBuilder->makeInstance() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:105
Illuminate\Database\Eloquent\FactoryBuilder->make() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:83
Illuminate\Database\Eloquent\FactoryBuilder->create() at /home/vagrant/php/housing/database/seeds/UsersTableSeeder.php:24
UsersTableSeeder->run() at /home/vagrant/php/housing/vendor/laravel/framework/src/Illuminate/Database/Seeder.php:42
结果是
$encoded_data = "This is a huge string";
$filename = "tempFile";
$handle = fopen($_SERVER['DOCUMENT_ROOT'].$filename, "a+")
file_put_contents($_SERVER['DOCUMENT_ROOT'].$filename, $encoded_data);
$file = new UploadedFile($_SERVER['DOCUMENT_ROOT'].$filename, $filename, null, filesize($_SERVER['DOCUMENT_ROOT'].$filename));
var_dump($file->getClientSize());
var_dump($file->getError());
var_dump($file->isValid());
var_dump(is_uploaded_file($file));
我确信tempFile存在于文档根目录中!
答案 0 :(得分:1)
is_uploaded_file()
会检查该文件是否已实际上传,因此无法使用您的代码,因为文件尚未上传。这就是isValid()
也返回false的原因,因为它使用is_file_uploaded()
来检查文件。
一旦这样说,UploadedFile确实有一个$ test参数,如果设置为true,将使isValid()
不检查文件是否实际上传。这用于测试,其中没有实际上传文件。您可以将此参数设置为true,然后isValid()
将为您的文件返回true。这是一个黑客,但也许是你需要的