Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a problem for many file uploads with the request structure for the file variables in an array. the following is the code snippet. i need a solution for this, thanks
$cimage = count($request->variant[$loop]['img']);
for($loops1 = 0; $loops1 < $cimage; $loops1++) {
$variantImage = $request->file($request->variant[$loop]['img'][$loops1]);
$nameVariantImage = $variant->id . '-' . date('ymdHis') . ($loops1 + 1) . '.' . $variantImage->getClientOriginalExtension();
$uploadVariant = $imageUpload->upload($variantImage, $nameVariantImage);
$variantimages = New ProductImage;
$variantimages->product_id = $product->id;
$variantimages->product_variant_id = $variant->id;
$variantimages->images = $uploadVariant;
$variantimages->save();
}
It's better using foreach for the looping, so based your request data the code will be like:
foreach ($request->variant as $varian) {
foreach ($varian['img'] as $k => $image) {
$nameVariantImage = $variant->id.'-'.date('ymdHis').($k + 1).'.'
.$image->getClientOriginalExtension();
$filePath = Storage::putFileAs(
"products/variant",
$image,
$nameVariantImage
);
$variantimages = new ProductImage;
$variantimages->product_id = $product->id;
$variantimages->product_variant_id = $variant->id;
$variantimages->images = $filePath;
$variantimages->save();
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 18 days ago.
Improve this question
I have 100 html files, and i want to add a line of code (at the end) to all of them.
How is this possible ?
Something like a script which appends that line into all html files
I tried searching but did not find anything.
You can use :
<?php
$folder = '/path/to/html/files';
$line_to_add = '<p>your line/p>';
foreach (glob("$folder/*.html") as $file) {
$contents = file_get_contents($file);
file_put_contents($file, $line_to_add . PHP_EOL . $contents);
}
Your question is quite unclear, because you didn't specify where you are going to append this lane? Here is a starting point:
<?php
$path = '/path/to/html/files';
$code_to_add = '<p>This line was added by a script.</p>';
$files = scandir($path); // get an array of all the files in the directory specified by $path.
foreach ($files as $file) { // Iterate over files
if (substr($file, -5) == '.html') { // hecking if each file has a ".html" extension.
$file_path = $path . '/' . $file;
$file_contents = file_get_contents($file_path); // read the contents
file_put_contents($file_path, $file_contents . $code_to_add); // put new content, you can modify it.
}
}
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am moving images from xcode to an online platform and i need to generate a JSON file from a directory structure (images only) that lists the folders, and the images in those folders, with complete URL (online) AND date they where added.
I am a COMPLETE noob with PHP web stuff, so am really lost at the moment.
I found the following code, but that does not do anything so far, and does not travels into directory's.
<?php
/*
JSON list of all images files in current directory
*/
$dir = ".";
$dh = opendir($dir);
$image_files = array();
while (($file = readdir($dh)) !== false) {
$match = preg_match("/.*\.(jpg|png|gif|jpeg|bmp)/", $file);
if ($match) {
$image_files []= $file;
}
}
echo json_encode($image_files);
closedir($dh);
?>
The following code works for me. Found it here.
I've only modified the echo, added the regex and the time. I hope this answers your question.
<?php
function getDirContents($path) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$files = array();
foreach ($rii as $file) {
if (!$file->isDir() && preg_match("/.*\.(jpg|png|gif|jpeg|bmp)/", $file)) {
$files[] = [
'path' => $file->getPathname(),
'c_time' => $file->getCTime()
];
}
}
return $files;
}
header('Content-Type: application/json');
echo json_encode([
'success' => true,
'files' => getDirContents('.')
]);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Whenever I try to upload files to my website I get 500 error and notification that server couldn't handle request. I tried to configure upload_max_filesize in both, php.ini and .htaccess, but nothing works. I also tried to set value of MaxRequestLen in apache2.conf:
<IfModule mod_fcgid.c>
MaxRequestLen 20000000
</IfModule>
EDIT: I can't post whole code because it contains sensitive data, here are parts of it:
$files = $_FILES["images"]["name"];
$tmpNames = $_FILES["images"]["tmp_name"];
$archiveName = time();
$folder = "./uploads/";
$price = 0;
foreach ($imagesFormats as &$imageFormat) {
if (!file_exists($folder.$archiveName."/".$imageFormat)) {
if (!mkdir($folder.$archiveName."/".$imageFormat, 0777, true)) {
addError("Error while trying to create directory.");
}
}
}
foreach ($_FILES["images"]["error"] as $key => $err) {
if ($err == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["images"]["tmp_name"][$key];
$name = basename($_FILES["images"]["name"][$key]);
move_uploaded_file($tmp_name, $folder.$archiveName."/".$imagesFormats[$key]."/".$name);
}
}
if (Zip($folder.$archiveName."/", $folder.$archiveName.".zip")) {
rrmdir($folder.$archiveName."/");
} else {
addError("Error on archiving.");
}
Problem is solved - I didn't knew that mb_strlen() function is not available by default in PHP but should be installed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a site where users can upload multiple images i am having problems as the new uploaded files are replacing the existing files in the images directory that has the same name. please help i am new to php
if (isset($_FILES['images']))
{
foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name)
{
$target = "../uploads/";
$target = $target.$_FILES['images']['name'][$key];
if(move_uploaded_file($tmp_name, $target))
{
$fname=$_FILES['images']['name'][$key];
$target = "PostAd/uploads/". $fname;
mysql_query("INSERT INTO `upload_data`(`clid`, `id`, `Imgpath`,`target`) VALUES ('$clid','$id','$fname','$target')");
$target="";
}
}
if ($sql){
header( "Location: /Myconnec/PostAd/CampusLife/confirm.php?clid=$clid");
ob_end_flush();
} else {
$error_msg = 'ERROR: Problems arose during the information exchange, please try again later.';
}
you can add a timestamp like this...
if (isset($_FILES['images'])) {
foreach ($_FILES['images']['tmp_name'] as $key => $tmp_name)
{
$target = $target.$_FILES['images']['name'][$key];
$ext = pathinfo($target, PATHINFO_EXTENSION);
$rand = rand();
$strip_ext = substr($target, 0, strlen($target) - strlen($ext));
$timestamp = str_replace(" ","",microtime());
$target = "$strip_ext.$timestamp.$rand.$ext";
if(move_uploaded_file($tmp_name, "../upload/$target"))
{
$fname=$_FILES['images']['name'][$key];
$target = "PostAd/uploads/".$target;
mysql_query("INSERT INTO `upload_data`(`clid`, `id`, `Imgpath`,`target`) VALUES ('$clid','$id','$fname','$target')");
$target="";
}
}
if ($sql){
header( "Location: /Myconnec/PostAd/CampusLife/confirm.php?clid=$clid");
ob_end_flush();
} else {
$error_msg = 'ERROR: Problems arose during the information exchange, please try again later.';
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've my .ini file as follows
languages.ini
--------------------
fr = "French"
en = "English"
Now, I'd like to read the file and keep the keys and values in the form of an array using PHP. I'm very new to PHP. I hope any one help me in achieving this. This is my following code.
public function GetLanguages()
{
$langdir = ISC_BASE_PATH.'/language';
$skip = Array (
'.',
'..',
'CVS',
'.svn',
);
$langs1 = array();
$dh = opendir($langdir);
while (($file = readdir($dh)) !== false) {
if (!is_file($langdir.'/'.$file.'/languages.ini')) {
continue;
}
$langs1[] = $file;
}
echo "FileLL:".$file;
foreach ($langs1 as $key) {
echo "Store languagesss::".$key."<br>";
}
return $langs1;
}
Thank you in advance.
You are looking for PHP's ini parse function:
parse_ini_file
Just call $arrayResult = parse_ini_file($path_to_file)