im pretty new to PHP scripting, and i couldnt find the answer to my desires yet, so i was hoping someone could tell me how to do this.
Im getting a $_POST request to a php file, containing some data
stock_nr=1&manufacturer=Audi&model=A1&images=http%3A%2F%2Fimages0.wheeler.nl%2Fs1386081049%2F6911582-1-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081072%2F6911582-2-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081102%2F6911582-3-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081131%2F6911582-4-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081161%2F6911582-5-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081192%2F6911582-6-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081227%2F6911582-7-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081255%2F6911582-8-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081280%2F6911582-9-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081313%2F6911582-10-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081341%2F6911582-11-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081368%2F6911582-12-2-2.jpg%2Chttp%3A%2F%2Fimages0.wheeler.nl%2Fs1386081398%2F6911582-13-2-2.jpg
I have made a file to declare variables like this:
$stock_nr=$_POST['stock_nr'] ;
$manufacturer=$_POST['manufacturer'] ;
$model=$_POST['model'] ;
$images=$_POST['images'] ;
How can i get the amount of imagelinks which are posted in a variable?
Edit: This is what i wanted to do, and it works fine.
This is what i wanted to do, and it does what it should do:
function process_images() {
$images = explode(',', $_POST['images']);
foreach($images as $image_nr => $image_url) {
$filename = 'images/'. $_POST['stock_nr'] .'-'. $image_nr .'.jpg';
$imgdata = file_get_contents($image_url);
file_put_contents($filename, $imgdata);
}
}
These links are separated by "," you just need to explode() it.
$images=explode(',',$_POST['images']);
print_r($images);
Related
I have an html file and i have done the code for printing the names of images from a folder in PHP , but it is not printing the values to screen. if i echo any other thing it is printed on screen.
i have the set the server using xampp.
how to correct this, i make the names of images from that folder printed on screen?
do i need to do anything extra
im a newbie in php. how can i achieve this?
<?php
function findImagesInFolder()
{
$folder = $_GET['C:\xampp\htdocs\firstsite'];
$images = glob($folder . '/*.{png,jpg,jpeg,gif}', GLOB_BRACE);
echo json_encode($images);
exit();
}
?>
It seems you have misused the $_GET variable.
I thing you want to just use the string value, ie.:
$folder = 'C:\xampp\htdocs\firstsite';
And note, the exit() terminates whole script after printing the json_encoded string.
So, to make the function reusable change it to get the path as a parameter $folder:
<?php
function findImagesInFolder($folder, $filter = '*.*')
{
// note, here we expect the path does not trail with backslash
$images = glob($folder . '\\' . $filter, GLOB_BRACE);
return json_encode($images);
}
// call the function with actual path to scan:
$path = 'C:\xampp\htdocs\firstsite';
$filter = '{*.png,*.jpg,*.jpeg,*.gif}';
echo findImagesInFolder($path, $filter);
?>
Note: output as [] means empty array encoded to JSON.
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);
}
I am trying to copy multiple images from a remote host (using a URL) to my local box (using XAMPP on my local box to execute the script).
I am using copy(). When I go to execute the copy(), only the LAST image in the array is created. So, if I have 5 image links, only the 5th image gets created and nothing prior even gets a file created.
I have tried CURL and FOpen and then both create all of the files, but all of the files are blank except, again, the last file which is perfectly fine.
$txt_file = file_get_contents('urls_for_images.txt');
if(!empty($txt_file)){
$image_links = explode("\n", $txt_file);
$i = 1;
foreach($image_links as $image_link){
$file_info = pathinfo($image_link);
copy($image_link, 'images/00' . $i . '_original.' . $file_info['extension']);
$i++;
}
}
I am not sure where the problem is occurring, but it seems odd to me that it will copy the last image in the text file, but not any of the others.
Thanks for the help in advance!
The i variable never changes, therefore the code tries to copy a file with the same name over and over again and only the last file is saved.
Try modifying your code this way:
$txt_file = file_get_contents('urls_for_images.txt');
if(!empty($txt_file)){
$image_links = explode("\n", $txt_file);
$i = 1;
foreach($image_links as $image_link){
$file_info = pathinfo($image_link);
copy($image_link, 'images/00' . $i . '_original.' . $file_info['extension']);
$i++;
}
}
You'd be better off with just file(), which reads the file into an array automatically:
$files = file('urls_for_images.txt', FILE_IGNORE_NEW_LINES);
foreach($files as $remote_file) {
$local_file = ....;
copy($remote_file, $local_file);
}
It appears the problem is the text file which line endings are \r\n and you are exploding with only \n. The quickest way to fix this is either explode by \r\n; or trim with default parameters to remove \r from the end of each line.
foreach($image_links as $image_link){
$image_link = trim($image_link);
$file_info = pathinfo($image_link);
...
}
However, the cleanest way to do this is to use file function, which handles line endings automatically. I recommend you to use this approach.
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.
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.