Can Not Save File Inside Tmp Directory

Can not save file inside tmp directory

absoluteString is not the correct method to get a file path of
an NSURL, use path instead:

let targetPath = tempDirectoryURL.URLByAppendingPathComponent("\(name).jpg").path!
data.writeToFile(targetPath, atomically: true)

Or better, work with URLs only:

let targetURL = tempDirectoryURL.URLByAppendingPathComponent("\(name).jpg")
data.writeToURL(targetURL, atomically: true)

Even better, use writeToURL(url: options) throws
and check for success or failure:

do {
try data.writeToURL(targetURL, options: [])
} catch let error as NSError {
print("Could not write file", error.localizedDescription)
}

Swift 3/4 update:

let targetURL = tempDirectoryURL.appendingPathComponent("\(name).jpg")
do {
try data.write(to: targetURL)
} catch {
print("Could not write file", error.localizedDescription)
}

Cannot save file in 'tmp' folder

Heroku doesn't behave like a virtual server. By design, dynos are disposable.

Every time you use heroku run you access a one-off dyno that is discarded as soon as the command ends (or you log out in the case of something interactive like bash). You are running your createfile.py script and bash in two completely unrelated instances that do not share a filesystem.

I'm not clear on what you are trying to achieve, but in general you can keep temporary "working" things in the memory of a single process for a short period of time. Anything you wish to persist beyond that scope must be saved outside of your dyno, e.g. in a client-server database like PostgreSQL or an object store like Azure Blob Storage.

Note that this model is a deliberate architectural decision and it impacts all tiers of Heroku dynos. Upgrading to a paid plan won't change anything.

PHPWord not saving file in /tmp directory

(1) Please try changing

$objWriter->save(sys_get_temp_dir() . '/helloWorld.docx');

to

$objWriter->save('./helloWorld.docx');

(2) Make sure you have write permission to your current folder.

(3) Run the program and see whether the helloWorld.docx is created in your current folder

How to save files in temp directory for any platform?

The temp directory is available via

String tempDir = System.getProperty('java.io.tmpdir')

Unable to save the uploaded file into specific directory

You're almost there.

File file = upFile.getFile(); is the temporary File you're getting through the form input. All you've got to do is move this file to your desired location by doing something like this: file.renameTo(ftemp).

Your problem in your code is that you're creating a bunch of files in memory ftemp and f1, but you never do anything with them (like writing them to the disk).

Also, I recommend you to clean up your code. A lot of it does nothing (aforementioned f1, also the block where you're doing the setWritable's). This will make debugging a lot easier.

Store file in directory tmp on heroku Rails

Heroku uses what is called an ephemeral filesystem. This means that your local filesystem is only accessible to a single dyno, and once the dyno is stopped, restarted, or moved, all files on the local filesystem are destroyed.

The only way for your Delayed Job process to transfer files to an outside process would be to store the files in a more permanent location. This could be S3, a database, Rackspace Files, etc, but for Heroku it cannot be the local filesystem.

However, if you are just looking to store the file in a temporary scratch location, it is fine to use the local filesystem. It looks like you may be having issues because the /app/tmp directory may not exist.

Try adding this to your worker:

Dir.mkdir(Rails.root.join('tmp'))

Or, add the tmp directory to your git repository.

mkdir tmp
touch tmp/.keep
git add tmp/.keep
git commit -m "Add tmp directory to app repository."

Working with files inside /tmp directory

As you can read here:

On most systems, this directory is cleared out at boot or at shutdown by the local system.

But as the documentation states, there is not much specified at the level of Linux, it depends on the distribution. Most servers don't reboot frequently, so the /tmp folder can become a mess that uses some (significant amount of) diskspace. Most Linux systems however allow to set the TMPTIME variable: the number of days a file can survive in /tmp. So by setting the variable to 1 files older than one day, are removed automatically (although probably not one second after they turn one day old). See this answer for more details.

Short answer: the files are part of the file system. They normally are not removed automatically, only at reboot or when they are old enough. This to ensure that when you have to perform a few actions on the files, they don't disappear in the middle of it. Although one of course never fully knows.

You thus should make sure you give your files different names: if two processes happen concurrently, they should not interact. For instance use a random number so that the first file is named zip151323.zip and the second zip4745443233.zip.



Related Topics



Leave a reply



Submit