Call to a member function getExtension() on null [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 days ago.
Improve this question
I'm trying to import excel file to MySQL (already install phpspreadsheet) in CodeIgniter 4. This is my code:
public function importExcel() {
$spreadsheet = new Spreadsheet();
$file = $this->request->getFile('excel');
$ext = $file->getExtension();
if ($ext === "xls" || $ext === "xlsx") {
if ($ext === "xls") $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
else $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spread = $reader->load($file);
$sheet = $spread->getActiveSheet()->toArray();
foreach ($sheet as $index=>$item) {
if ($index == 0) continue;
$data =
[
'id'=>$item[0], // kolom 0
'nama'=>$item[1],
......
];
$this->rhModel->insert($data);
}
session()->setFlashdata('import','Data berhasil di-impor');
}
else {
session()->setFlashdata('error','Bukan format file excel');
}
return redirect()->to('/cv');
}
and I get this error :
Call to a member function getExtension() on null" and stuck in line code : $file = $this->request->getFile('excel');
This the code calling above function :
<form action="/imporExcel" method="post" enctype="multipart/form-data">
<br>
<div class="btn-group" role="group" >
<label for="files" class="btn">Pilih file excel</label>
<input id="files" type="file" accept=".xls, .xlsx, .ods">
</div>
<button type="submit" class="btn" style="width:90px;height: 30px; background-color:#90e1f5; color:black">
<i class="fa-sharp fa-solid fa-upload"></i>Import
</button>
</form>
What's wrong with my code ?

Related

upload multiple files on laravel [closed]

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();
}
}

My PHP is not uploading videos [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
My HTML code is this:
<div id="videoinput" class="inputbox">
<form autocomplete="off" action="../upload/" name="textinput" method="POST" enctype="multipart/form-data">
<input type="text" id="videotitlebox" name="texttitle" required class="inputtext" placeholder="Title of video" />
<textarea type="text" id="textdescbox" name="textdesc" required class="inputtext" placeholder="Description"></textarea>
<?php
require("../common/category.php");
?>
<input type="file" name="uploadvideo" value="Upload video" id="videoupload" required />
<input type="submit" name="uploadvideo" value="Submit" class="submitbut" />
</form>
My PHP code is this:
if (isset($_POST['uploadvideo'])) {
if($_POST['category']!="" && $_POST['category']!="selectcategory") {
define("UPLOAD_DIR1", "../uploadedfiles/");
$myFile = $_FILES["myFile"];
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['file']['error']);
}
$info = getimagesize($_FILES['file']['tmp_name']);
if ($info === FALSE) {
die("File is not of an acceptable type. Only .GIF, .JPEG,.JPG and .PNG files are acceptable.");
}
if (($info[2] !== IMAGETYPE_GIF) && ($info[2] !== IMAGETYPE_JPEG) && ($info[2] !== IMAGETYPE_PNG)) {
die("File is not an image file.");
}
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
$temp = explode(".", $name);
$name = $_SESSION['user_id'] . '.' . end($temp);
// don't overwrite an existing file
$parts = pathinfo($name);
$id = $_SESSION['user_id'] . Date("__h.m.s__d.m.y__") . "image";
$id = md5($id);
$name = md5($id) . "." . $parts["extension"];
// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR1 . $name);
chmod(UPLOAD_DIR1 . $name, 0644);
$time = date("h:i:s");
$mydate=getdate(date("U"));
$date = "$mydate[year].$mydate[mon].$mydate[mday]";
$title = $_POST['texttitle'];
$category = $_POST['category'];
$description = $_POST['textdesc'];
$sql = "INSERT INTO uploads(title, category, description, path, id, time, date, username, datetime, type)
VALUES (:title, :category, :description, :path, :id, :time, :date, :user, :datetime, :type)";
$query = $conn->prepare($sql);
$query->execute(array(':title'=>$title, ':category'=>$category, ':description'=>$description, ':path'=>$name, ':id'=>$id, ':time'=>$time, ':date'=>$date, ':user'=>$_SESSION['user_id'], ':datetime'=>$date . " " . $time, ':type'=>"image") );
echo "Success.";
header('Location: ../home/');
}
else {
}
}
Problem is that my code never even gets to the third line. As soon as the code starts, it dies because of the first if which goes "$_FILES['file']['error'] !== UPLOAD_ERR_OK".
Why is this happening? How do I upload the video?
Any other suggestions to clean up my PHP code is much appreciated.
Spot the differences:
<input type="file" name="uploadvideo" value="Upload video" id="videoupload" required />
^^^^^^^^^^^
$myFile = $_FILES["myFile"];
^^^^^^
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
^^^^^^

PHP code to download images from xml file to local folder [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I wanted to download all the images in an xml file (fabfurnish.xml) to a local folder (output). After editing the code below according to #user1978142 i got the desired result. Thanks.
<?php
$xml = simplexml_load_file('input/fabfurnish.xml'); // your xml
foreach($xml->product as $url) {
$url = (string) $url->image;
$filename = basename($url); // get the filename
if(file_exists($filename)) {
echo "file <strong>$filename</strong> already exists <br/>";
} else {
$img = file_get_contents($url); // get the image from the url
file_put_contents('output/'.$filename, $img); // create a file and feed the image
echo "file <strong>$filename</strong> created <br/>";
}
}
?>
This is the actual xml file i am working with:
https://www.wetransfer.com/downloads/a88c2605cf038f8cc72603a0cf33904620140711034115/2ce9841bc9fd57063a0919de5f46862120140711034115/c1adbb
Actually, this is quite straightforward. Sample:
$xml = simplexml_load_file('fabfurnish.xml'); // your xml
foreach($xml->product as $url) {
$url = (string) $url->image;
$filename = basename($url); // get the filename
if(file_exists($filename)) {
echo "file <strong>$filename</strong> already exists <br/>";
} else {
$img = file_get_contents($url); // get the image from the url
file_put_contents($filename, $img); // create a file and feed the image
echo "file <strong>$filename</strong> created <br/>";
}
}
If you want to parse specific xml file like your sample, you can edit as following code in getImagesFromXML($xmlurl).
foreach ($xml->product as $product) {
foreach($product->image as $url) {
$tmp = explode("/", $url);
save($url, end($tmp), "output/");
}
}

Build an array from .INI file using PHP [closed]

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)

how do I Import CSV file in mysql using php and codeigniter? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am developing a website for a sales company. I want to add a bulk data of employees which will be a CSV file. I want help in doing so. A code sample will be much appreciated as I'm new in this. This code should upload the CSV first and then import it to MySQL. Thanks in advance!
Have a view with a file upload form, then have a csv_model with an upload function called by the controller. This function should contain code which looks a bit like this:
if (isset($_FILES['userfile']) && $_FILES['userfile']['size'] > 0 && $_FILES['userfile']['error'] == 0) {
// Upload the file:
if (!is_dir($this->upload_path)) mkdir($this->upload_path,0777,TRUE);
$config['upload_path'] = $this->upload_path;
$config['allowed_types'] = 'csv';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo 'Error: '; print_r($error); die();
return $error;
}
else
{
// It's uploaded, so open it, loop through it and do what you need to do
$data = array('upload_data' => $this->upload->data());
$file_path = $data['upload_data']['full_path'];
$row = 1;
$db_row = array();
if (($handle = fopen($file_path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$db_row[$row]['xxx'] = $data[0];
// Any database insertion goes here...
}
}
}}

Categories