Issue with the moving of file which is successfully uploaded - php

Please find below the coded section,
You can give your inputs based on the interpretation.
public function upload(Request $request)
{
$content = $_POST['code'];
if (Storage::exists('file.blade.php'))
{
echo "File is already exists..............";
}
else
{
Storage::disk('local')->put('file.blade.php', $content);
echo "uploaded successfully...........";
$fileName = "file.blade.php";
$oldPath = "/storage/app/file.blade.php";
$destinationPath = "/resources/views";
File::move($oldPath, $destinationPath);
}
}
I have used the above code trying to move the file. But I got the following error message.
ErrorException in Filesystem.php line 176:
rename(/storage/app/file.blade.php,/resources/views): The system
cannot find the path specified. (code: 3)

Try changing like this:
$fileName = "file.blade.php";
$oldPath = "/storage/app/file.blade.php";
//$destinationPath = "/resources/views"."$fileName";
$destinationPath = "/resources/views/"."$fileName";

Related

File_get_contents(): failed to open stream: Connection refused

I am trying to download a previously uploaded file, form database and uploads folder in php codeigniter. I am using code below but downloading empty file.
controller.php
public function downloadFile($incidents_id)
{
$file = $this->incidents_model->getfile($incidents_id);
//echo $file; die;
$this->load->helper('download');
$path = file_get_contents(base_url()."uploads/".$file); //(the error shows on this line)
// echo $path; die;
$name = $file; // new name for your file
// echo $name ; die;
force_download($name, $path); // start download`
}
incidents_model.php
function getfile($Incident_id )
{
$file = $this->db->select('file');
$this->db->from('incidents');
$this->db->where('incidents_id' , $Incident_id );
$query = $this->db->get();
// return $query->row();
if ($query->num_rows() > 0) {
return $query->row()->file;
}
return false;
}
view.php
<div class="form-group col-md-4">
Download file
</div>
so running this code download an empty file.
echo $file; die; displays the file name which been saved in db and in uploads folder
echo $path; die; generates an error:
Severity: Warning
Message:
file_get_contents(http://localhost:8080/ticketing_tool_v2/uploads/Screenshot
2021-03-04 at 5.59.38 PM.png): failed to open stream: Connection
refused
Filename: admin/Incidents.php
Line Number: 380
Reviewing the documentation for file_get_contents you'll observe there's many different ways you can use it. For your purposes you would need to allow inbound connections to the filesystem.
The other way you could do this for better future proofing is to use the CodeIgniter file helper - https://codeigniter.com/userguide3/helpers/file_helper.html
Before reading the file from path, please check whether a path points to a valid file.
public function downloadFile($incidents_id) {
try {
$this->load->helper('download');
$file = $this->incidents_model->getfile($incidents_id);
$data = file_get_contents(base_url("uploads/" . $file));
$name = $file; // new name for your file
force_download($name, $data); // start download`
} catch (Exception $e) {
//exception handling code goes here
print_r($e);
}
}

finfo_file(C:\xampp\tmp\phpC546.tmp): failed to open stream: No such file or directory

Yii2:
I am updating a pdf file, but when doing the action I get this error.
The path in the source code is created, just like the file is saved in the created path, and in the database.
but after performing this process I get this error
Script PHP-Yii2
// Update PDF
$pathPdf = 'uploads/pdf/userSettings/';
if (!is_dir($pathPdf)) {
mkdir($pathPdf, 0777, true);
}
if(UploadedFile::getInstance($model, 'file_pdf')){
$model->file_pdf = UploadedFile::getInstance($model, 'file_pdf');
$file = $pathPdf . md5($model->company_name) . '.' . $model->file_pdf->extension;
$model->pdf_front_path = $file;
}
if (!$model->validate()) {
$errors = $model->errors;
$this->showErrorMessages($tab);
} else {
if ($model->save()) {
if (UploadedFile::getInstance($model, 'file_pdf')) {
$model->file_pdf->saveAs($file);
}
} else {
Yii::$app->session->setFlash('error', Yii::t('app', 'error_save'));
}
}
Could you help me, I have tried several things but nothing has worked for me
You can solve it simply by moving the line ''$model->file_pdf->saveAs($file);'' under '''$model->pdf_front_path = $file;''' for some strange reason Yii jumps that error with the routes, it is as if it did a double validation of this route, but on the second occasion it validates it no longer finds it.
the code stayed like this
// Update PDF
$pathPdf = 'uploads/pdf/userSettings/';
if (!is_dir($pathPdf)) {
mkdir($pathPdf, 0777, true);
}
if(UploadedFile::getInstance($model, 'file_pdf')){
$model->file_pdf = UploadedFile::getInstance($model, 'file_pdf');
$file = $pathPdf . md5($model->company_name) . '.' . $model->file_pdf->extension;
$model->pdf_front_path = $file;
$model->file_pdf->saveAs($file);
}
after that you can already perform the validations you want, but they are not from the route

how to upload and save qif file using php

I am trying to upload a .qif file in php codeigniter but it returns an error
The filetype you are attempting to upload is not allowed.
When I try to upload another type file (PDF, CSV, docs, etc.) they upload successfully.
Here is my code:
function do_upload($field_name, $files, $folder_path,$save_name="",$prefix="bk_"){
$ci = & get_instance();
//create upload folder if not exists
if (!is_dir($folder_path)) {
mkdir($folder_path, 0777, TRUE);
}
$save_name = $prefix.time()."_".$files['name'];
$data = array();
$config = array();
$config['upload_path'] = $folder_path;
//$config['max_size'] = 0;
$config['allowed_types'] = 'csv|CSV|txt|TXT|pdf|PDF|zip|ZIP|doc|DOC|docx|DOCX|xlsx|xls|XLS|XLSX|QIF|qif';
$config['file_name'] = $save_name;
$ci->load->library('upload');
$ci->upload->initialize($config);
// echo "hello 1"; die;
if ($ci->upload->do_upload($field_name)){
$data = $ci->upload->data();
$data['status'] = 1;
}
else{
$data['status'] = 0;
$data['error'] = $ci->upload->display_errors();
}
return $data;
}
You get the "filetype is not allowed" error, because the original Codeigniter mime-type configuration file doesn't list an qif entry:
in your config/mimes.php file add to the $mimes array this line:
'qif' => 'application/qif'
and eventually
'qif' => 'image/x-quicktime'
mime-type source: http://fileformats.archiveteam.org/wiki/Ext:qif
the native php move_uploaded_file method without checking for mime-types can turn into a security problem
I got a solution after lot of searches
I just use move_uploaded_file function and it's work
move_uploaded_file($_FILES["file"]["tmp_name"], $path);
if someone have a better answer answer please share that.
thanks

The file was not uploaded due to an unknown error

I am trying to upload same image for multiple students. It works for first one but for next one it gives error.
my code
foreach($student_ids as $key => $student_id) {
$fileIds='';
if(Input::hasfile('attachment')){
$comment = $comments[0];
$file = Input::file('attachment');
$destinationPath = public_path().'/uploads/moments/'.$student_id;
$filename = "moment_".time()."_".trim(rand(1,999)).".".$file->getClientOriginalExtension();
if(is_dir($destinationPath)) {
$upload_success = $file->move($destinationPath, $filename);
}else {
if(mkdir($destinationPath)) {
$upload_success = $file->move($destinationPath, $filename);
}
}
$file = Moment_gallery::create(['image'=>$filename,'comment'=>$comment]);
$fileIds.=$file->id.',';
}
$moments = new moment();
$moments->activity_id = $activity;
$moments->type=$activity_type;
$moments->student_id=$student_id;
$moments->date = $date;
$moments->time = $activity_time ? $activity_time : ' ';
$moments->save();
}
here i am getting array of multiple students id... So i am just uploading same image for those students. It works for single student but then for multiple students it works for first one and give error for second one as
FileException in UploadedFile.php line 235:The file "student.png" was not uploaded due to an unknown error.
whats wrong..? Thank you.
$file = Input::file('attachment');
Understanding that $file is of object type:
Illuminate\Http\UploadedFile
and that it's pointing at the file on the server, e.g.
e‌‌cho $file->getRealPath(); // ‌/tmp/phpp6HYIk
then you can see that by performing:
$file->move($dest_path);
you're moving the file away from it's original destination thereby making it unavailable for subsequent iterations.

How to copy an image from one folder to another using php

I'm having difficulty in copying an image from one folder to another, now i have seen many articles and questions regarding this, none of them makes sense or work, i have also used copy function but its giving me an error. " failed to open stream: No such file or directory" i think the copy function is only for files. The image i wanna copy is present in the root directory. Can anybody help me please. What i am doing wrong here or is there any other way???
<?php
$pic="somepic.jpg";
copy($pic,'test/Uploads');
?>
You should write your code same as below :
<?php
$imagePath = "/var/www/projectName/Images/somepic.jpg";
$newPath = "/test/Uploads/";
$ext = '.jpg';
$newName = $newPath."a".$ext;
$copied = copy($imagePath , $newName);
if ((!$copied))
{
echo "Error : Not Copied";
}
else
{
echo "Copied Successful";
}
?>
You should have file name in destination like:
copy($pic,'test/Uploads/'.$pic);
For your code, it must be like this:
$pic="somepic.jpg";
copy($pic,'test/Uploads/'.$pic);
Or use function, like this:
$pic="somepic.jpg";
copy_files($pic,'test/Uploads');
function copy_files($file_path, $dest_path){
if (strpos($file_path, '/') !== false) {
$pathinfo = pathinfo($file_path);
$dest_path = str_replace($pathinfo['dirname'], $dest_path, $file_path);
}else{
$dest_path = $dest_path.'/'.$file_path;
}
return copy($pic, $dest_path);
}

Categories