multiple upload php - php

Im stuck on a problem,
I have created a multiply uploader, it works fine until i want the file I upload to register the name inside a database,
The error message I get is
Warning: mysql_real_escape_string() expects parameter 1 to be string,
array given in C:\wamp\www\bookstyled\profile.php on line 16
My line 16 is the variable file_name
$file_name = mysql_real_escape_string($_FILES['file_name']['name']);
If I remove the mysql_real_escape_string, It actually save to the database but not as the file name, but its says " Array "
This is some of the code
if(isset($_FILES['file_name'])) {
foreach ($_FILES['file_name'] ['tmp_name'] as $key => $tmp_name){
$file_name = mysql_real_escape_string($_FILES['file_name']['name']);
$dt1=date('y-m-d H:m:s');
$ip=$_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO `files` (`file_name`, `user_name`,`file_time`,`file_ip`) VALUES ('$file_name', '{$_SESSION['username']}','$dt1','$ip')") ;
move_uploaded_file($tmp_name, "core/files/{$_FILES['file_name']['name'][$key]}");
}
}
And If I didn't mention it
The files are being upload.
Thanks

You have enabled multiple uploads. So I'm guessing your html names for the fields are arrays. eg:
<input type="file" name="file_name[]" multiple="multiple">
Now $_FILES['file_name']['name'] doesn't hold one file but multiple files in an array.
Each file is individually accessed through
$_FILES['file_name']['name'][$i] //where $i is a 0,1,2.....
Since you are using
$file_name = mysql_real_escape_string($_FILES['file_name']['name'])
the function mysql_real_escape_string isn't being given a string as the parameter but the complete array which hold each and every file uploaded to 'file_name'.
The solutions is simple, you need to use
file_name = mysql_real_escape_string($_FILES['file_name']['name'][$key])
$key because I see that you are already using that in move_upload_file function

If you change this
$file_name = mysql_real_escape_string($_FILES['file_name']['name']);
to this
$file_name = mysql_real_escape_string($_FILES['file_name']['name'][$key]);
As you use this [$key] in this sentence move_uploaded_file($tmp_name, "core/files/{$_FILES['file_name']['name'][$key]}"); I think you should use on the line 16 too.

Looks like $_FILES['file_name']['name'] is an array as opposed to a string, so try $_FILES['file_name']['name']['key']

Untested, but could work.
if(isset($_FILES['file_name'])) {
foreach ($_FILES['file_name'] as $file){
$file_name = mysql_real_escape_string($file['tmp_name']);
$dt1=date('y-m-d H:m:s');
$ip=$_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO `files` (`file_name`, `user_name`,`file_time`,`file_ip`) VALUES ('$file_name', '{$_SESSION['username']}','$dt1','$ip')") ;
move_uploaded_file($tmp_name, "core/files/{$file['name']}");
}
}

Related

Replace Array value with other preg_replaced value

i've got an array, which i know that its values would be JPGs from somewhere
i need to go to each value returned to that array and preg_replace some characters
then set the values of the returned values to some other value
here's the code and here's what i've tried
//first piece of code
$data['images'] = array();
foreach ($single['PictureURL'] as $ispec) {
$data['images'][] = $ispec;
$ispec = preg_replace('/\$_[0-9]+.JPG\b/i', '$_10.JPG', $ispec);
$file = 'C:\wamp64\www\mz\images1.txt';
file_put_contents ($file, $ispec, FILE_APPEND);
//images1.txt shows all images returned fine with modified strings
}
//second piece of code
$product->imageUrl = $data['images'][0];
unset($data['images'][0]);
$product->subImageUrl = $data['images'];
$file = 'C:\wamp64\www\mz\images3.txt';
file_put_contents ($file, $data['images'], FILE_APPEND);
//images3.txt shows all the images returned but without being modified?? WHY??!
the first piece of the code is working on all values and replacing is working just fine.
the second piece of the code is my issue, it is returning the values of the old none modified images, which i don't
i need to modify the images before its being written to
'$product->imageUrl & $product->subImageUrl'
The problem is very simple. You're modifying your data after you already
stored it in $data['images']. To solve this, just move this line to
after the preg_replace:
foreach ($single['PictureURL'] as $ispec) {
$ispec = preg_replace('/\$_[0-9]+.JPG\b/i', '$_10.JPG', $ispec);
$data['images'][] = $ispec;
$file = 'C:\wamp64\www\mz\images1.txt';
file_put_contents ($file, $ispec, FILE_APPEND);
}

laravel | How to replace a field in form's request?

I am using laravel 5.4 and I'm trying to replace the imagePath field in my request (renaming the uploaded image).
explanation:
when the form is submitted the request field(request->imagePath) contains the temporary location of the uploaded image, I am moving that tmp image to a dir while changing its name ($name). so now as the request->imagePath still has old tmp image location I want to change request->imagePath value to have the new location and then create the user.
Like so
if($request->hasFile('imagePath'))
{
$file = Input::file('imagePath');
$name = $request->name. '-'.$request->mobile_no.'.'.$file->getClientOriginalExtension();
echo $name."<br>";
//tried this didn't work
//$request->imagePath = $name;
$file->move(public_path().'/images/collectors', $name);
$request->merge(array('imagePath' => $name));
echo $request->imagePath."<br>";
}
But Its not working, Here is the output
mahela-7829899075.jpg
C:\xampp\tmp\php286A.tmp
Please Help
I believe merge() is the correct method, it will merge the provided array with the existing array in the ParameterBag.
However, you're accessing the input variables incorrectly. Try using $request->input('PARAMETER_NAME') instead...
Therefore, your code should look like this:
if ($request->hasFile('imagePath')) {
$file = Input::file('imagePath');
$name = "{$request->input('name')}-{$request->input('mobile_no')}.{$file->getClientOriginalExtension()}";
$file->move(public_path('/images/collectors'), $name);
$request->merge(['imagePath' => $name]);
echo $request->input('imagePath')."<br>";
}
Note: You can also pass your path into public_path() and it will concatenate it for you.
References
Retrieving Input:
https://laravel.com/docs/5.4/requests#retrieving-input
$request->merge():
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Http/Request.php#L269
public_path: https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/helpers.php#L635

PHP concatanate variable name

In Propriete entity I have 5 field called image1 image2 image3 image4 image5
I wan't to add these fields in a for loop
I tried this but it doesn't work:
for($i=0;$i<count($this->request->data['files'])&&$i<5;$i++){
//... some code
$propriete->{'image'.$i+1} = $file['name'];
}
}
Can someone help me?
EDIT
This is the code of my loop:
for($i=0; $i<count($this->request->data['files']) && $i<5; $i++){
$file=$this->request->data['files'][$i];
$ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
if(in_array($ext, $arr_ext))
{
//do the actual uploading of the file. First arg is the tmp name, second arg is
//where we are putting it
move_uploaded_file($file['tmp_name'], WWW_ROOT . '/img/' . $file['name']);
debug($file['name']);
//prepare the filename for database entry
$propriete->{'image'.$i+1} = $file['name'];
}
}
Your code should work fine, you're just missing parenthesis which makes your concatenation go haywire
$propriete->{'image'.($i+1)}="test";
This can also be demonstrated by this simple test
$i=2;
echo 'image'.$i+1; // 1 :)
VS
echo 'image'.($i+1); //image3
Put the value of every method in a variable to store the value and then use it.For eg
$values = $this->request->data['files'];
You cannot for loop the data without putting them in a variable first.
If you cannot find the filename the problem should be that you are naming the object property in the wrong manner.You should enclose $i+1 with quotes.

php Update filename from directory

so the title is not full clear, my question , I'm using the code to rename the file from directory present in the server the problem is i have to use the HTML form and php to update the file name, i want to do this : there will be an option on every file for renaming it when i click on the option the box pops up and i have to type the new name for file and save it , any help will be appreciated. (before down voting think about the question.)
The code that I'm using to update the file name
<?php
include("configuration.php");
$target = $_POST['filename'];
$newName = $_POST['newfilename'];
$actfoler = $_REQUEST['folder'];
$file = "files/users/";
$new ="files/users/";
$renameResult = rename($file, $new);
// Evaluate the value returned from the function if needed
if ($renameResult == true) {
echo $file . " is now named " . $new;
} else {
echo "Could not rename that file";
}
header("Location:".$_SERVER["HTTP_REFERER"]);
?>
Try changing these lines:
$file = "uploads/$loggedInUser->username$actfolder/$target";
$new ="uploads/$loggedInUser->username$actfolder/$newName";
To:
$file = "uploads/{$loggedInUser->username}{$actfolder}/{$target}";
$new ="uploads/{$loggedInUser->username}{$actfolder}/{$newName}";
To explain why:
You are using variables inside a string, which means you will want to tell PHP where the variable ends. Especially when referencing objects or arrays, but also when you are placing variables right next to each other. I'm guessing PHP evaluated your original line to uploads/[Object]->usernamePizza/newname
I don't think you can call object properties in a string as you do.
try replace these lines :
$file = "uploads/".$loggedInUser->username."$actfolder/$target";
$new ="uploads/".$loggedInUser->username."$actfolder/$newName";
You may think about echoing $file and $new to confirm the path is nicely built.
On a side note, I'd recommend to really check the entries because this code can obviously lead to major security issues.

Counting sent files

I have an multiple input sending files and I need guard this images with another name inside my folder called 'home';
So the pictures filing with the name home1.jpg, home2.jpg, etc
So, here is my code:
$file = $_FILES['Filedata'];
$filename_home = "";
$img_array = array($filename);
foreach($img_array as $key=>$value){
$filename_home.="home".$key.".jpg";
}
But this doesn't producing the result.
Any help, will be appreciate
Where does $filename come from? It looks like you want to use $file instead.

Categories