Call to member function getClientOriginalExtension() in laravel? - php

try multiple file upload in Laravel so my code for view is
<input type="file" name="photos">
but i facing this problem to cant exact type .. ?
$img = $request->file('photos');
$fileExtension=$img->getClientOriginalExtension();

If you are posting single image. You try this code :-
if($request->hasFile('photos')){
if (Input::file('photos')->isValid()) {
$file = Input::file('photos');
$destination = 'images/Foldername'.'/';
$ext= $file->getClientOriginalExtension();
$mainFilename = str_random(6).date('h-i-s');
$file->move($destination, $mainFilename.".".$ext);
echo "uploaded successfully";
}
}
Make sure You have added the enctype in form:-
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="photos">
</form>
If you are uploading multiple images. Try this code:-
if ($request->hasFile('photos')) {
$files = $request->file('photos');
foreach($files as $file){
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$fileName = str_random(5)."-".date('his')."-".str_random(3).".".$extension;
$destinationPath = 'images/Foldername'.'/';
$file->move($destinationPath, $fileName);
}
}
And form must look like this :-
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="photos[]" multiple>
</form>
Hope it helps!

In $img you got an array. And you shoud use loop like foreach to operate with $img

Related

Notice: Undefined index: file when uploading file

I am trying to do a file check in an MVC framework, and even if I put the file with a jpg format, I am getting error
This is the function that checks the format
public function addAction()
{
$upload = new Upload();
if($this->request->isPost())
{
$allowed = array('gif','png' ,'jpg');
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed))
{
echo 'error';
}
$this->request->csrfCheck();
$upload->assign($this->request->get());
$upload->user_id = Users::currentUser()->id;
if($upload->save())
{
Router::redirect('upload');
}
}
$this->view->uploas = $upload ;
$this->view->displayErrors = $upload->getErrorMessages();
$this->view->postAction = PROOT . 'upload' . DS . 'add';
$this->view->render('upload/add');
}
And this is the HTML code:
<div class="col-lg-6 upload-position center" >
<input type="file" id="file" name="file" >
</div>
The isPost method checks if the form method is post
For the next one looking for it :)
To upload a file you will need enctype="multipart/form-data":
<form enctype="multipart/form-data">
<input type="file" name="file"/>
</form>

How to use foreach for multiple image upload using php and mysql

Here is my Form:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file[]" id="file" />
<input type="submit" value="Upload Image" name="submit">
</form>
Here is my php:
<?php
$file=$_FILES['file']['name'];
$dest="uploads/$file";
$src=$_FILES['file']['tmp_name'];
move_uploaded_file($src,$dest);
?>
How to use foreach after hitting the submit button? kindly guide me please.
MY FOR EACH GIVES ONLY ONE VALUE. I UPLOADED MORE THAN TWO IMAGE.iT SHOWS LAST ONE
foreach($_FILES['file']['name'] as $k=>$v)
{
echo "File : ", $_FILES['file']['name'][$k] ," is valid, and was successfully uploaded.\n";
}
Try this ...
foreach ($_FILES['image']['tmp_name'] as $key => $val ) {
$filename = $_FILES['image']['name'][$key];
$filesize = $_FILES['image']['size'][$key];
$filetempname = $_FILES['image']['tmp_name'][$key];
$fileext = pathinfo($fileName, PATHINFO_EXTENSION);
$fileext = strtolower($fileext);
// here your insert query
}
hi your question seems incomplete but i'll try to answer
an a example to process
foreach($file as $oneFile){
mysql_query("INSERT INTO {{your table name}} VALUES ("+oneFile+")")
}
but it work only if you have initialise a connexion to mysql

move_uploaded_file in PHP not working?

I'm actively learning php and am working on a CMS Project. I'm stuck on image upload.
PHP
if ( $_POST['img'])
$uploads_dir = '/images';
$tmp_name = $_FILES["img"]["tmp_name"];
$name = $_FILES["img"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
HTML
<img src="images/$image" />
MySQL
$sql = "INSERT INTO affordplan VALUES('$title','$name','$bodytext','$created')";
return mysql_query($sql);
The name of the file is uploaded to database but the file itself is not being uploaded to the destination folder.
Type your html code in between form and in the form wrtie as following
<form action="" method="post" enctype="multipart/form-data">
<img src="images/$image" name="img" />
...
</form>
Use $_FILES to check file is posted or not instead of $_POST. Also make proper quote for variables. Then for echo php variable use php tags.
Try
PHP:
if ( $_FILES['img'])
$uploads_dir = 'images'; // will be on same location where php file exist.
$tmp_name = $_FILES["img"]["tmp_name"];
$name = $_FILES["img"]["name"];
move_uploaded_file($tmp_name, $uploads_dir.'/'.$name);
HTML:
<img src="images/<?php echo $image;?>" />
MySQL:
$sql = "INSERT INTO affordplan VALUES('$title','$name','$bodytext','$created')";
return mysql_query($sql);
// Upload multiple files to the folder
$upload_dir = '/images';
if ( $_FILES['img']){
foreach ($_FILES["img"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["img"]["tmp_name"][$key];
$name = $_FILES["img"]["name"][$key];
move_uploaded_file($tmp_name, "$upload_dir/$name");
}
}
}
//MySQL
MySQL:
$sql = "INSERT INTO affordplan VALUES('$title','$name','$bodytext','$created')";
return mysql_query($sql);
HTML used in form submission
<input type="file" name="img" multiple>
Showing the images in HTML
$dir = "/images/";
$images = glob($dir."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
You are assigning a directory that is equivalent to the file name. Try this
<?php
if (!file_exist("your main directory/the file to story")) {
mkdir("your main directory/the file to story", 0777, true);
}
// then you start uploading your once the folder is created
?>
The process here is if the folder directory doesn't exist, the mkdir() function will create that folder. Then that's the time you start on moving the file to the created folder.
Put all of your code in if statement:
if ( $_POST['img']) {
$uploads_dir = '/images';
$tmp_name = $_FILES["img"]["tmp_name"];
$name = $_FILES["img"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
<form action="phpfilename.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" value="Upload Image" name="submit">
$file=$_FILES['file']['name'];
$dest="uploads/$file";
$src=$_FILES['file']['tmp_name'];
move_uploaded_file($src,$dest);
$result=mysql_query("insert into tablename(dbfieldname) values('$dest')");

move_uploaded_file wont work on my local machine

I'm new in PHP area. This is my try to upload a file:
<?php
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
if($file['error'] === 0) {
$distination = 'uploads/';
$file_ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$filename = $distination . uniqid('', true) . '.' . $file_ext;
if(!move_uploaded_file($file['name'], $filename)) {
echo "File upload failed!";
}
}
}
?>
<form action="testupload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
the return value from the move_uploaded_file always false. I create the uploads folder in the same directory as the upload script file.
Your main problem is fact that you use name instead of tmp_name value from $_FILES array.
name is original name of uploaded file, tmp_name is location of file after upload. Manual Page
Fixed version below. I also added check for destination directory and automatic creation of it if not available.
<?php
if(isset($_FILES['file'])) {
$file = $_FILES['file'];
if($file['error'] === 0) {
$distination = 'uploads/';
if(!file_exists($distination)){
mkdir($distination, 0777, true);
}
$file_ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$filename = $distination . uniqid('', true) . '.' . $file_ext;
print_r($file);
if(!move_uploaded_file($file['tmp_name'], $filename)) {
echo "File upload failed!";
}
}
}
?>
<form action="testupload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>

Uploading files with PHP

I am using a form for users to upload files to my website. I want to allow them to upload multiple photos at once, so I am using the HTML5 "multiple" attribute.
My HTML:
<form method="post" action="save.php">
<input type="file" name="uploads[]" multiple="multiple" />
<input type="submit" name="submit" value="submit"/>
</form>
save.php:
<?php
foreach ($_FILES['uploads']['name'] as $file) {
echo $file . "<br/>";
$file= time() . $_FILES['uploads']['name'];
$target= UPLOADPATH . $file;
move_uploaded_file($_FILES['uploads']['tmp_name'], $target)
or die('error with query 2');
}
But, for some reason when I run the script, I get an error saying undefined index: uploads. And an error saying that I have an invalid argument supplied for foreach(). What could I be dong wrong?
Thanks
UPDATE
Okay, setting the enctype="mulitpart/form-data" worked. Now, I am having trouble with moving the file. I am getting the error move_uploaded_file() expects parameter 1 to be string, array given. What am I doing wrong here?
Thanks again
You need the proper enctype to be able to upload files.
<form method="post" enctype="multipart/form-data" action="save.php">
try this html code: <form method="post" action="save.php" enctype="multipart/form-data">
Then in PHP:
if(isset($_FILES['uploads'])){
foreach ($_FILES['uploads']['name'] as $file) {
echo $file . "<br/>";
$file= time() . $_FILES['uploads']['name'];
$target= UPLOADPATH . $file;
move_uploaded_file($_FILES['uploads']['tmp_name'], $target)
or die('error with query 2');
}
} else {
echo 'some error message!';
}
In order to upload files in the first place, you need enctype="multipart/form-data" on your <form> tag.
But, when you upload multiple files, every key in $_FILES['uploads'] is an array (just like $_FILES['uploads']['name']).
You need to get the array key when looping, so you can process each file. See the docs for move_uploaded_file for more deatils.
<?php
foreach ($_FILES['uploads']['name'] as $key=>$file) {
echo $file."<br/>";
$file = time().$file;
$target = UPLOADPATH.$file;
move_uploaded_file($_FILES['uploads']['tmp_name'][$key], $target)
or die('error with query 2');
}
index.html
<form method="post" action="save.php" enctype="multipart/form-data">
<input type="file" name="uploads[]" multiple="multiple" />
<input type="submit" name="submit" value="Upload Image"/>
</form>
save.php
<?php
$file_dir = "uploads";
if (isset($_POST["submit"])) {
for ($x = 0; $x < count($_FILES['uploads']['name']); $x++) {
$file_name = time() . $_FILES['uploads']['name'][$x];
$file_tmp = $_FILES['uploads']['tmp_name'][$x];
/* location file save */
$file_target = $file_dir . DIRECTORY_SEPARATOR . $file_name;
if (move_uploaded_file($file_tmp, $file_target)) {
echo "{$file_name} has been uploaded. <br />";
} else {
echo "Sorry, there was an error uploading {$file_name}.";
}
}
}
?>

Categories