I am trying to create folder when it does not exist. If folder is exist it will skip and continue to create the next folder.
Which part is wrong in this following code,
Error
Micrsoft Jscript runtime error: File already exists
Code
function CreateFolder(fldr)
{
if (fso.FolderExists(fldr)){
return;
}
else
fso.CreateFolder("C:\\"+ fldr);
}
If fldr doesn't include the drive letter, FolderExists looks for this folder in the current wording directory. But your code then creates this folder in C:\. Most likely, the error occurs because there's no folder with this name in the current working directory, but it exists in C:\.
Your code should probably be either
function CreateFolder(fldr)
{
if (! fso.FolderExists(fldr))
fso.CreateFolder(fldr);
}
or
function CreateFolder(fldr)
{
var path = fso.BuildPath("C:", fldr);
if (! fso.FolderExists(path))
fso.CreateFolder(path);
}
Related
I am trying to upload a file and move it to public/ folder. The file uploads without problem to writable folder, however, it is the moving to the public folder that has a problem.
Here is my code;
$update_post->move(ROOTPATH.'public/', $update_post.'.'.$fileType);
Path is correct. When I echo out echo ROOTPATH.'public/'; and then manually copy/paste, I do get to the destination directory.
Permissions correct. There are my permission on the public/ directory:
drwxr-xr-x 9 www-data www-data 4096 Jan 30 01:08 public
Any hints appreciated.
Reason:
It's because the move(string $targetPath, ?string $name = null, bool $overwrite = false) method's $name argument is invalid.
$update_post->move( ... , $update_post.'.'.$fileType);
Explanation:
Concatenating an class CodeIgniter\Files\File extends SplFileInfo instance calls the inherited SplFileInfo class's __toString() method which returns the path to the file as a string.
Note that it doesn't return the filename, which is what you're interested in.
Solution:
You should instead pass in the basename instead.
$update_post->move(
ROOTPATH . 'public/',
$update_post->getBasename()
);
Alternatively, since you're not changing the destination filename, it's cleaner to just not pass in the second parameter of the move(...) method. I.e:
$update_post->move(
ROOTPATH . 'public'
);
Addendum:
If you wish to change the destination filename to a new name, try this instead:
guessExtension()
Attempts to determine the file extension based on the trusted
getMimeType() method. If the mime type is unknown, will return null.
This is often a more trusted source than simply using the extension
provided by the filename. Uses the values in app/Config/Mimes.php to
determine extension:
$newFileName = "site_logo"; // New filename without suffixing it with a file extension.
$fileExtension = $update_post->guessExtension();
$update_post->move(
ROOTPATH . 'public',
$newFileName . (empty($fileExtension) ? '' : '.' . $fileExtension)
);
Notes:
The move(...) method returns a new File instance for the relocated file, so you must capture the result if the resulting location is needed: $newRelocatedFileInstance = $update_post->move(...);
I am facing a weird issue with Laravel 5.5. I am working on my local xampp server and trying to execute LOAD DATA INFILE query in controller function. But I am getting error PDOException (HY000)
SQLSTATE[HY000]: General error: 29 File 'D:\xampp\mysql\data\uploads\RDMST.csv' not found (Errcode: 2 "No such file or directory")
Currently my csv file is located at public/uploads folder. I tried various path settings for filename like
$fileCsv = 'uploads/RDMST.csv';
$fileCsv = public_path().'/uploads/RDMST.csv';
But I am getting same error and please notice the error path shows mysql\data in path. I am not getting from where it is appending this two folders in path. When I echo public path , it displays this:
D:\xampp\htdocs\tax\public
So, can you please help how to use path for load file without appending this "mysql\data" in path?
Any help is greatly appreciated.
Here is my complete controller function:
public function getCsv()
{
//echo public_path(); die;
//$fileCsv = public_path().'/uploads/RDMST.csv';
$fileCsv = 'uploads/RDMST.csv';
echo $query = "LOAD DATA INFILE '".$fileCsv."'
REPLACE
INTO TABLE tblreception
(year,
reception_number,
file_date,
file_time,
instrument_type,
#created_at,
#updated_at)
SET created_at=NOW(),updated_at=null";
DB::connection()->getPdo()->exec($query);
//return view('reception');
}
Can we not use csv file path?
Thanks
I've copied a .bat-file from Qt-ressources to a file system and executed it.
After that I wanted to delete the file, but it fails in Qt. If fails also when I restart the application. However, the file can be removed in the file-explorer.
I tried QFile::remove as well as QDir::remove. Static as well as not-static versions - no effect.
I tried to call using native file-separator - didn't help either.
What is wrong with this code?
if ( QFileInfo( dataRootPath+"/backupdb.bat" ).exists() )
{
//debugger stepps in
QFile f( QFileInfo( dataRootPath+"/backupdb.bat" ).canonicalFilePath());
f.remove( );
}
I had the same problem copying file from resources to file system and trying to remove it after that. QFile::errorString() returns "Access denied". So it seems that resource file has some nasty permissions that are copied by QFile::copy. May be it's possible to change permissions but I used my own 2 functions to copy file:
bool copyTextFile(QString srcPath, QString dstPath)
{
QFile file(srcPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
return writeTextFile(QString::fromUtf8(file.readAll()), dstPath);
}
bool writeTextFile(QString data, QString dstPath)
{
QFile file(dstPath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
QTextStream stream(&file);
stream << data;
return true;
}
I change its permissions before remove it.
QFile::copy(":/res/1.txt", "D:\\1.txt");
QFile file("D:\\1.txt");
file.setPermissions(file.permissions() |
QFileDevice::WriteOwner |
QFileDevice::WriteUser |
QFileDevice::WriteGroup |
QFileDevice::WriteOther);
file.remove();
I encountered the same error, but in my case the posted solutions did not work. However, it turned out that I had created a std::ofstream object in my code, that was unclosed. Thus, this was keeping the source file open which prevented the copying on Windows.
I have to write gradle task which will be copying files. Files are stored at tests/[Name]/test.txt and for each Name I want to create numbered directory /tested/test00/, /tested/test01/ etc. and in each catalog should be one file (test.txt from source folder renamed to test00, test01 etc.)
I have the code, but behavior is strange...
It creates correct directories /tested/test00 etc. but all files in each directory have the same name... test06. So number in directory is correct but in file name it isn't.
My code is:
int copyTaskIterator = 0
int testIterator = 0
...
sources.each { mySource ->
task "myCopyTask$copyTaskIterator"(type: Copy)
nameSuffix = String.format("%02d", testIterator)
fromPath = 'tests/'+mySource+'/test.txt'
toPath = "tested/test"+nameSuffix
tasks."myCopyTask$copyTaskIterator".from fromPath
tasks."myCopyTask$copyTaskIterator".into toPath
tasks."myCopyTask$copyTaskIterator".rename { fileName ->
fileName.replace '.txt', nameSuffix
}
preBuild.dependsOn tasks."myCopyTask$copyTaskIterator"
copyTaskIterator++
testIterator++
}
The problem is, that nameSuffix is evaluated too late. Sadly no documentation explains whether it is executed in execution time or not.
Just try to use rename(java.util.regex.Pattern, java.lang.String)
tasks."myCopyTask$copyTaskIterator".rename("\\.txt", nameSuffix)
I'm using laravel 4 and I need to change an uploaded image, I have it in:
Public
--uploads
---news
----id_news.jpg
When editing the new's I want to make a change image form, but how could I delete and upload another file. I am using:
Input::file('img')->move('uploads/news', $id.'_news.jpg');
The problem it's that it doesn't work, it's not replacing the file, so how could I delete The image so I could upload again.
In laravel 3 I only used:
File::delete('path/to/file');
But I don't see anything about removing files in laravel docs.
I think you should append public_path() to file names , to get real file path, like this
File::delete(public_path().$id.'_news.jpg');
There is still the delete method in Laravel 4:
src/Illuminate/Filesystem/Filesystem.php
otherwise just use good old unlink()!?
You can easily do something like:
$filename = public_path().'/uploads/foo.bar';
if (File::exists($filename)) {
File::delete($filename);
}
Reference: Laravel-recipes Delete a File
Other delete usage:
// Delete a single file
File::delete($filename);
// Delete multiple files
File::delete($file1, $file2, $file3);
// Delete an array of files
$files = array($file1, $file2);
File::delete($files);
Source: http://laravel-recipes.com/recipes/133/deleting-a-file
$destinationPath = 'uploads/my-image.jpeg'; // from public/
if ( File::exists($destinationPath) ) {
File::delete($destinationPath);
}
This works on laravel 4.2.
File::delete(storage_path()."/ProductSalesReport-20150316.csv");
// Here are some other paths to directories laravel offers, Hope this
helps
/* Path to the 'app' folder */
echo app_path();
/* Path to the project's root folder */
echo base_path();
/* Path to the 'public' folder */
echo public_path();
/* Path to the 'app/storage' folder */
echo storage_path();