Image upload fails when using laravel 6.
Controller
public function saveProduct(Request $request){
/*image upload process */
$productimage = $request->file('image');
$imageName = $productimage->getClientOriginalName();
$directory = 'Product-image/';
$imageUrl = $directory.$imageName;
$productimage->move($directory,$imageName);
$product= new Product();
$product->category_id = $request->category_id;
$product->brand_id = $request->brand_id;
$product->product_price = $request->product_price;
$product->product_quantity = $request->product_quantity;
$product->shortdescription = $request->shortdescription;
$product->longdescription = $request->longdescription;
$product->image = $imageUrl;
$product->public_status = $request->public_status;
$product->save();
return redirect('/product/add')->with('message','Prodect added saved successfully');
Migration
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('category_id');
$table->integer('brand_id');
$table->float('product_price',8,2);
$table->integer('product_quantity');
$table->text('shortdescription');
$table->text('longdescription');
$table->text('image')->nullable();
$table->tinyInteger('public_status');
$table->timestamps();
});
Routes
Route::get('/product/add', 'ProductController#index')->name('addproduct'); Route::post('/product/new', 'ProductController#saveProduct')->name('newproduct'); Route::get('/product/manage', 'ProductController#manageProduct')->name('manageproduct');
Blade
<h3 class="text-center text-success">{{Session::get('message')}}</h3>
<form action="{{route('newproduct')}}" method="post" enctype="multipart/form-data">
#csrf
Following will resolve you're the query, By default, the maximum upload file size for PHP scripts is set to 128 megabytes. However, you may want to change these limits. For example, you can set a lower limit to prevent users from uploading large files to your site. To do this, change the upload_max_filesize and post_max_size directives in your php.ini file.
upload_max_filesize = 20M
post_max_size = 21M
To verify the current value of the upload_max_filesize directive and other directives, you can use the phpinfo() function.
You can also do this same by mentioning in you .htaccess file, if you just want to change these values for your project.
// Replace xx with the maximum upload file size that you want to set
php_value upload_max_filesize xxM
// Replace xx with the maximum HTTP POST file size that you want to set
php_value post_max_size xxM
Related
I need help when I try to upload an image
I get this error and I don't understand why
avatar is not a valid uploaded file.
my code in controller
rules:
'avatar' => [
'rules' => 'required|uploaded[avatar]|max_size[avatar,1024]|ext_in[avatar,jpg,jpeg,png]',
'errors' => [
'required' => lang('Core.Auth.Error.required'),
]
],
upload:
if (!$this->validate($rules)) {
$data['validation'] = $this->validator;
} else {
$avatar = $this->request->getFile('avatar');
$newName = $avatar->getRandomName();
$avatar->move(WRITEPATH.'uploads', $newName);
$filepath = base_url()."/selfie/".$newName;
my php.ini
upload_max_filesize = 1G
post_max_size = 1G
memory_limit = 1G
From CodeIgniter documentation:
The uploaded rule fails if the name of the parameter does not match the name of any uploaded files.
The exact error message you posted will appear if this rule cannot be satisfied
Double check your HTML <input> tag and make sure that both name attribute of this tag and the name passed to uploaded rule are the same, e.g.:
// In the HTML
<input type="file" name="avatar">
// In the controller
$this->validate([
'avatar' => 'required|uploaded[avatar]|max_size[avatar,1024]|ext_in[avatar,jpg,jpeg,png]'
]);
From another script i got some generated symlinks.
2QGPCKVNG1R -> /anotherdir/movie1.mp4
HJS7J9ND2L5 -> /anotherdir/movie2.mp4
LKA6A9LA7SK -> /anotherdir/movie3.mp4
Displaying these files in NGINX works fine, but I'd like to rename the files at download via content disposition.
Question is how do i get the original filename in nginx variable?
I'm not sure it is possible at all. Is that another script yours or under your control? You can generate an additional nginx config file with a map block with the same script where you can describe a ruleset for mapping an URI value to the Content-Disposition header value (or you can write an additional script to do it with readlink -f <symlink> command:
map $uri $content_disposition {
~/2QGPCKVNG1R$ movie1.mp4;
~/HJS7J9ND2L5$ movie2.mp4;
~/LKA6A9LA7SK$ movie3.mp4;
}
And then include that file to the main nginx config:
include /path/to/content-disposition-map.conf;
server {
...
add_header Content-Disposition $content_disposition;
Another way I see is to use lua-nginx-module and a LUA script like
map $symlink_target $content_disposition {
~/([^/]*)$ $1;
}
server {
...
set_by_lua_block $symlink_target {
local result = io.popen("/bin/readlink -n -f " .. ngx.var.request_filename)
return result:read()
}
add_header Content-Disposition $content_disposition;
I'm trying to upload a large file - 6GB to S3. The S3 file size limit is 15GB. When uploading the 6GB file the request fails with the response null in the error callback. I'm able to successfully upload a 5GB file. Is there a size limit for ng-file-upload? Do I need to use the resumeChunkSize feature, even though this file size is within the limits of S3's 15GB?
return Upload.http({
url: url,
method: 'PUT',
headers: headers,
data: file,
resumeChunkSize: '10MB' // necessary?
})
.progress(function(evt){
return evt;
})
.error(function errorCallback(response) {
return response; // null
})
When I try to upload a file more than about 100MB by yii\web\UploadedFile the file isn't uploaded. But all fields become empty and each field has the validation error:
field cannot be blank
This is my validation rules:
public function rules() {
return [
[['name_promo_file', 'description_promo_file', 'url_promo_file', 'size_promo_file', 'lang_promo_file'], 'required'],
[['date_upload_promo_file'], 'safe'],
[['size_promo_file'], 'integer'],
[['lang_promo_file'], 'string'],
[['name_promo_file', 'description_promo_file', 'url_promo_file'], 'string', 'max' => 255],
[['imagePromo'], 'file', 'skipOnEmpty' => false, 'extensions' => 'pdf doc png, jpg, jpeg']
];
}`
This is my php.ini setting
upload_max_filesize = 256M
post_max_size = 256M
max_execution_time = 60
How can I solve this?
I was facing the same problem but i solved by using kartiks file input widget and increasing the size in php.ini ,Here you please seperate the extensions as pdf,docx,....please do a reply if the problem is not solved.hope we can solve it.
i have a serious problem with php file upload.
the problem is that the file is not moving from the tmp to the destination directory
this is my html page
<form name="f1" method="post" action="handleUpload.php" enctype="multipart/form-data">
<input type="file" name="htmlfile">
<input type="submit">
</form>
and here is my PHP page handleUpload.php,
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$arrayKeys=array_keys($_FILES['htmlfile']);
foreach($arrayKeys as $s)
echo $s ." : ".$_FILES['htmlfile'][$s]." <br>";
$name = $_FILES['htmlfile']['name'];
$size = $_FILES['htmlfile']['size'];
$tempName= $_FILES['htmlfile']['tmp_name'];
list($filePureName, $ext) = explode(".", $name);
$valid_formats = array("jpg","png", "gif", "bmp","rar","zip");
$fileObject=fopen($tempName,"r") ;
echo "test Text" . fgets($fileObject) ."</br>";
fclose($dstFile) ;
if($_FILES["htmlfile"]["error"] != 0 ) {
echo "file error with code " .$_FILES["htmlfile"]["error"];
}
if( is_uploaded_file($tempName)){
echo "<br> upladed to the tmp directory ";
move_uploaded_file($_FILES["htmlfile"]["tmp_name"],"./".$_FILES["htmlfile"]["name"]) ;
//move_uploaded_file($_FILES["htmlfile"]["tmp_name"],"upload/".$_FILES["htmlfile"]["name"])
}
}
else{
echo "you must visit the HTML upload page first";
}
the output i get is
name : numerical_hw1.rar
type : application/octet-stream
tmp_name : /tmp/phpK7Gmmf
error : 0
size : 34642
test TextRar!Ïs WÅt K„Á &–Gá#3& numerical_hw1\hw1_q1\numerical_hw1.cpp°¸ EÌÌý•½¦~g†¡¹[•Í…Àk#€Qu{Q|0a®'#ÆàM¹%_ûánšF“Q‘Á®ÞÛÅ€ÁEq'‰\—ç9yq%Páðà;áy¹‰ùLx_,Xrh»qüÙ‰ÐO‰ †3JI²†Žc÷“4ˆ ‰‡oɲE³*[⦆sÓè¤Í~·Ö”?ø‘ŒK6"Á¦æF,¼JÇ1rê¼´…BžT5$_sôJäļ†Å$ý˦A:`ë5kÎ'3_„}ªœRijÅ3ÎÜøsÛV:÷5ËΰãÚÓžõÌÚK¦œñÕ÷ÚßýTvÙäÓ
upladed to the tmp directory
the problem is that the file is not moved to the current directory nor to the "upload" directory -of course when it was alone and not commented- !!
as you can see the file is uploaded successfully to the tmp directory and i have open it, and read a line from it. and also i have checked it with the is_uploaded_file function and it pass that check.
i have carefully read php file upload topics from many sites, and found nothing is missing in both php and html !!
the folder permission is 755.
i should mention here that file uploading was working before recently, and i am sure that no body has changed or modified the php.ini file.
Any Help Please?? what should i do?
after exploring the server carefully i found that my website has been hacked, and the hacker upload a hack file !
but really i don't know what to do now and also i cant estimate the damage size caused by that hacker so Any Suggestion ?!!!!
Try that:
$pathDestino=$_SERVER['DOCUMENT_ROOT'] . "\\xxx\\upload\\" . $_FILES["htmlfile"]["tmp_name"];
$tmp_name=$_FILES["htmlfile"]["tmp_name"];
move_uploaded_file($tmp_name, $pathDestino);