moving uploaded file to a dynamic table in php - php

I'm trying to move the file that I uploaded using move_uploaded_file. Here are my variables:
$filename = $_FILES['File_file']['name'];
$folder_id = $_POST['File']['folder_id'];
$folder_name_result = $this->filemanager_model->getfoldername($folder_id);
$fileloc = $_FILES['File_file']['tmp_name'];
$folder_name = "";
foreach ($folder_name_result->result_array() as $row)
{$folder_name = $row['title'];}
$pathAndName = "filemanager/".$folder_name."/".$filename;
And the outputs of the variables:
$folder_name = Grrr
$pathAndName = filemanager/Grrr/cis.png
$fileloc = C:\xampp1.8\tmp\phpE21E.tmp
When I run the move_uploaded_file function, it generates an error where:
move_uploaded_file(filemanager/Grrr/cis.png): failed to open stream: No such file or directory
move_uploaded_file(): Unable to move 'C:\xampp1.8\tmp\php2565.tmp' to 'filemanager/Grrr/cis.png'
My filepaths:
/admin - base_url
/admin/filemanager/Grr - The folder I want it to save
/admin/application/controllers/- the path of my controllers
Is there something wrong with my code as to why it's not working?

I just had something wrong with the formatting of my file path. I had to include the exact path since I'm on localhost, so once I put this on the server I've got to change path to the one on my server.
$pathAndName = "C:\\xampp1.8/htdocs/cicubecms/admin/filemanager/".$cat_name."/".$folder_name."/".$filename;

Related

PHP copy command folder

My currency working directory is ../main
I have a script which accepts files to be uploaded.
I have the following code but instead of coping the file into the ../_assets folder, it copies it into the ../main folder with the filename as ../_assets/correct.csv.
Any idea what might be wrong here?
Thanks
const IMPORT_FILE_DIR = "..\_assets\\";
private $file_name = "";
$this->file_name = Page::IMPORT_FILE_DIR . $_FILES['file_in']['name'];
$result= copy( $_FILES['file_in']['tmp_name'],$this->file_name);
Here fileName is = "..\_assets\correct2.csv"

Warning : file_put_contents() failed to open stream : No such file or directory

I am trying to upload an image from android to mysql
Now the path can be successfully uploaded but there's something wrong on this code
<?php
require_once '../database/database.php';
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// base_64 encoded string from android
$imageData = $_POST['image'];
// edittext from android
// $imageName = $_POST['image_name'];
$path = "images/Sample.jpg";
$actualPath = "http://192.168.254.123/*****/admin/$path";
if($user->UploadFiles($actualPath))
{
file_put_contents($path, base64_decode($imageData));
echo "Success";
}
else
{
echo "Failed";
}
}
?>
On the line where file_put_content is the error . here's the full error
Warning file_put_contents(images/Sample.jpg):failed to open stream:No such file or directory in C:\xampp\htdocs\projectname\admin\apk_api\upload_profile.php on line 17
Here's the proof that I can actually save the path on my database
and here's my directory
Can someone please help me out.
The directory images/ does not exists from the point of view, where the admin\apk_api\upload_profile.php file is. The resulting directory path will be
C:\xampp\htdocs\projectname\admin\apk_api\images\
However, the target directory should be
C:\xampp\htdocs\projectname\admin\images\
Create a relative path based from the location of the admin\apk_api\upload_profile.php file. You have to use something like ../images/ to get to the target directory.

how can i get the working path of my image in codeigniter when uploading

I have a code in codeigniter which i will store the path of my image in my database. It works fine in inserting but the path is not correct.
My folder name of my upload images are stored in upimages.
The upimages is located in outside of application folder in codeigniter where i created assets/upimages. Here is my path code in inserting to database:
$data = $this->upload->data();
$image_path = $data['full_path'];
//var_dump($image_path);
$userid = ($this->session->userdata['logged_in']['user_id']);
$imagedb = $this->prof_model->user_img($image_path,$userid);
if(file_exists($image_path)) {
$status = "success";
$msg = "File successfully uploaded";
}
my image_path is the full path of my image but it outputs none if i call it again and here is the output of the full_path in my database:
C:/xampp/htdocs/Uploadtest/assets/upimages/test.jpg
when i change the path in my db and change it to :
http://localhost/Uploadtest/assets/upimages/test.jpg
it works fine.
my question is. how can i adjust my full_path to come up with the 2nd path. Remember that the location of my upimages is outside of the application folder which i created an asset/upimages
Try to make your file path as like as following:
$image_url = base_url().'assets/upimages/'.$data['upload_data']['file_name'];
$imagedb = $this->prof_model->user_img($image_url,$userid);

How to save file in subfolder using php

I have a script with a mysql query which saves a file called invoice.xml every day automatically by running a cron job. In case no data is found a no_orders.txt is saved.
I would like this file not be saved to the same folder as the script.php file is in but to a subfolder called invoices.
The renaming of the old invoice.xml is done with the following code
// rename old file
$nowshort = date("Y-m-d");
if(file_exists('invoice.xml')) {
rename('invoice.xml','invoice_'.$nowshort.'.xml');
}
The saving is done with the following code:
if($xml1 !='') {
$File = "invoice.xml";
$Handle = fopen($File, 'w');
fwrite($Handle, $xml1);
print "Data Written - ".$nowMysql;
fclose($Handle);
#print $xml;
die();
} else {
print "No new orders - ".$nowMysql;
$File = "no_orders_".$nowshort.".txt";
$Handle = fopen($File, 'w');
fclose($Handle);
die();
}
Could I please get assistance how to save this file to a subfolder. Also the renaming of the existing file would need to be within the subfolder then. I have already tried with possibilities like ../invoice/invoice.xml but unfortunately without any success.
Thank you
Just give the path of file 'invoice.xml' to $File.
Otherwise create some $Dir object which will point to Folder named 'invoice', then use accordingly
Use __DIR__ magic constant to retrieve your script.php directory, then you can append /invoice/invoice.xml .
Example if path to your script php something like this:
/var/www/path/to/script.php
$currentDir = __DIR__; //this wil return /var/www/path/to
$invoicePath = $currentDir.'/invoice/invoice.xml';

On creating zip file by php I get two files instead of one

I'm struggling around with a simple PHP functionality: Creating a ZIP Archive with some files in.
The problem is, it does not create only one file called filename.zip but two files called filename.zip.a07600 and filename.zip.b07600. Pls. see the following screenshot:
The two files are perfect in size and I even can rename each of them to filename.zip and extract it without any problems.
Can anybody tell me what is going wrong???
function zipFilesAndDownload_Defect($archive_file_name, $archiveDir, $file_path = array(), $files_array = array()) {
// Archive File Name
$archive_file = $archiveDir."/".$archive_file_name;
// Time-to-live
$archiveTTL = 86400; // 1 day
// Delete old zip file
#unlink($archive_file);
// Create the object
$zip = new ZipArchive();
// Create the file and throw the error if unsuccessful
if ($zip->open($archive_file, ZIPARCHIVE::CREATE) !== TRUE) {
$response->res = "Cannot open '$archive_file'";
return $response;
}
// Add each file of $file_name array to archive
$i = 0;
foreach($files_array as $value){
$expl = explode("/", $value);
$file = $expl[(count($expl)-1)];
$path_file = $file_path[$i] . "/" . $file;
$size = round((filesize ($path_file) / 1024), 0);
if(file_exists($path_file)){
$zip->addFile($path_file, $file);
}
$i++;
}
$zip->close();
// Then send the headers to redirect to the ZIP file
header("HTTP/1.1 303 See Other"); // 303 is technically correct for this type of redirect
header("Location: $archive_file");
exit;
}
The code which calls the function is a file with a switch-case... it is called itself by an ajax-call:
case "zdl":
$files_array = array();
$file_path = array();
foreach ($dbh->query("select GUID, DIRECTORY, BASENAME, ELEMENTID from SMDMS where ELEMENTID = ".$osguid." and PROJECTID = ".$osproject.";") as $subrow) {
$archive_file_name = $subrow['ELEMENTID'].".zip";
$archiveDir = "../".$subrow['DIRECTORY'];
$files_array[] = $archiveDir.DIR_SEPARATOR.$subrow['BASENAME'];
$file_path[] = $archiveDir;
}
zipFilesAndDownload_Defect($archive_file_name, $archiveDir, $file_path, $files_array);
break;
One more code... I tried to rename the latest 123456.zip.a01234 file to 123456.zip and then unlink the old 123456.zip.a01234 (and all prior added .a01234 files) with this function:
function zip_file_exists($pathfile){
$arr = array();
$dir = dirname($pathfile);
$renamed = 0;
foreach(glob($pathfile.'.*') as $file) {
$path_parts = pathinfo($file);
$dirname = $path_parts['dirname'];
$basename = $path_parts['basename'];
$extension = $path_parts['extension'];
$filename = $path_parts['filename'];
if($renamed == 0){
$old_name = $file;
$new_name = str_replace(".".$extension, "", $file);
#copy($old_name, $new_name);
#unlink($old_name);
$renamed = 1;
//file_put_contents($dir."/test.txt", "old_name: ".$old_name." - new_name: ".$new_name." - dirname: ".$dirname." - basename: ".$basename." - extension: ".$extension." - filename: ".$filename." - test: ".$test);
}else{
#unlink($file);
}
}
}
In short: copy works, rename didn't work and "unlink"-doesn't work at all... I'm out of ideas now... :(
ONE MORE TRY: I placed the output of $zip->getStatusString() in a variable and wrote it to a log file... the log entry it produced is: Renaming temporary file failed: No such file or directory.
But as you can see in the graphic above the file 43051221.zip.a07200 is located in the directory where the zip-lib opens it temporarily.
Thank you in advance for your help!
So, after struggling around for days... It was so simple:
Actually I work ONLY on *nix Servers so in my scripts I created the folders dynamically with 0777 Perms. I didn't know that IIS doesn't accept this permissions format at all!
So I remoted to the server, right clicked on the folder Documents (the hierarchically most upper folder of all dynamically added files and folders) and gave full control to all users I found.
Now it works perfect!!! The only thing that would be interesting now is: is this dangerous of any reason???
Thanks for your good will answers...
My suspicion is that your script is hitting the PHP script timeout. PHP zip creates a temporary file to zip in to where the filename is yourfilename.zip.some_random_number. This file is renamed to yourfilename.zip when the zip file is closed. If the script times out it will probably just get left there.
Try reducing the number of files to zip, or increasing the script timeout with set_time_limit()
http://php.net/manual/en/function.set-time-limit.php

Categories