I'm trying to upload a file and place it in my web/uploads/produits/img directory but the code bellow is not working:
public function getUploadDir()
{
return 'uploads/produits/img';
}
protected function getUploadRootDir()
{
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
I get the folowing error:
Could not move the file "C:\wamp\tmp\php9265.tmp" to "C:\wamp\www\Projet\src\Arkiglass\ProduitBundle/../../../..\web/uploads/produits/img\." (move_uploaded_file()
[function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\php9265.tmp' to 'C:\wamp\www\Projet\src\Arkiglass\ProduitBundle/../../../..\web/uploads/produits/img\.')
It's seems like it doesn't know the directory __DIR__.'/../../../../web/'...
It doesn't seem your entity lives under the entity directory, but rather directly under the bundles dir. Does it? So you're going up one dir to much. Two options:
Move your entity in the Entity subfolder, adjust namespace and all references. Standard symfony bundle dir layout.
remove one level of ../
Related
I want to 'read' a .MD file inside the public folder (it's need to be in public folder because the user can change these MD files after the build).
File Structure: https://prnt.sc/pg72yq
I think in loading this MD file asynchronously with nodejs or PHP, but i wish load without that, maybe with AJAX, but i actually don't what exactly to do.
/*
Reads the page
*/
read(page){
let file = require(process.env.PUBLIC_URL + `./pages/${page}/index.md`);
// ... Code
}
I need to pass a File to a class for unit tests. The class requires a File specifically, and can't be modified - it just reads it. (So I don't want to try to mock out that class or modify it - just pass it the File it needs.)
What's the recommended way to do this?
I put the file in src/test/resources/... and just passed that entire path in, and, since the test is run in the project root dir, this works. But this seems quite wrong.
UPDATE: To emphasize - the class needs a File object, not an InputStream or anything else.
this.getClass().getResourceAsStream(fileName) is recommended way to read files from resources
If you need to look up something on the classpath and have it returned as a File, try this:
import java.net.URL
import java.io.File
object Test extends App {
val fileUrl: URL = getClass.getResource("Test.class")
val file : File = new File(fileUrl.toURI())
println(s"File Path: ${file.getCanonicalPath}")
}
It more or less uses all Java classes, but it works. In my example, the Test object is finding its own compiled classfile, but you can obviously change it to whatever you want.
The JVM doesn't have a concept of getting a classpath resource as a File because resources don't necessarily correspond to files. (Often they're contained in jars.)
Your test could start by getting an InputStream and copying it into a temporary file.
I have an application that needs to load an add-on in the form of a dll. The dll needs to take its configuration information from a configuration (app.config) file. I want to dynamically find out the app.config file's name, and the way to do this, as I understand , is AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
However, since it is being hosted INSIDE a parent application, the configuration file that is got from the above piece of code is (parentapplication).exe.config. I am not able to load another appdomain inside the parent application but I'd like to change the configuration file details of the appdomain. How should I be going about this to get the dll's configuration file?
OK, in the end, I managed to hack something together which works for me. Perhaps this will help;
Using the Assembly.GetExecutingAssembly, from the DLL which has the config file I want to read, I can use the .CodeBase to find where the DLL was before I launched a new AppDomain for it. The *.dll
.config is in that same folder.
Then have to convert the URI (as .CodeBase looks like "file://path/assembly.dll") to get the LocalPath for the ConfigurationManager (which doesn't like Uri formatted strings).
try
{
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
string originalAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
Uri uri = new Uri(String.Format("{0}\\{1}.dll", originalAssemblyPath, assemblyName));
string dllPath = uri.LocalPath;
configuration = ConfigurationManager.OpenExeConfiguration(dllPath);
}
catch { }
First of all: i'm a camel newbie :-)
I want to watch a directory for xml files, then i want to move that xml file to another directory and move a pdf file with the same filename (but other extention) to the same directory, and then do some java stuff.
What is the best way to move that pdf file?
This is the route that i currently have:
from("file://C:/temp/camel/in?delete=true").filter(new Predicate() {
#Override
public boolean matches(final Exchange exchange) {
String filename = (String) exchange.getIn().getHeader("CamelFileRelativePath");
return "xml".equals(FilenameUtils.getExtension(filename));
}
})
.to("file://C:/temp/camel/out").bean(ServiceBean.class, "callWebservice")
Thanks!
You can achieve that without using a filter but a regular expression to filter only the 2 extensions, .xml and .pdf
from("file://C:/temp/camel/in?delete=true&?include=.*.xml|.*.zip")
.to("file://C:/temp/camel/out").bean(ServiceBean.class, "callWebservice");
If you use filter it will delete the files that you are not interested in, which might not be what you want, this solution will just leave them in that directory
So I'm trying to dynamically create a folder inside the web pages folder.
I'm making a game database. Everytime a game is added I do this:
public void addGame(Game game) throws DatabaseException {
em.getTransaction().begin();
em.persist(game);
em.getTransaction().commit();
File file = new File("C:\\GameDatabaseTestFolder");
file.mkdir();
}
So everything works here.
The file get's created.
But I want to create the folder like this:
public void addGame(Game game) throws DatabaseException {
em.getTransaction().begin();
em.persist(game);
em.getTransaction().commit();
File file = new File(game.getId()+"/screenshots");
file.mkdir();
}
Or something like that. So it will be created where my jsp files are and it will have the id off the game.
I don't understand where the folder is created by default.
thank you in advance,
David
It's by default relative to the "current working directory", i.e. the directory which is currently open at the moment the Java Runtime Environment has started the server. That may be for example /path/to/tomcat/bin, or /path/to/eclipse/workspace/project, etc, depending on how the server is started.
You should now realize that this condition is not controllable from inside the web application.
You also don't want to store it in the expanded WAR folder (there where your JSPs are), because any changes will get lost whenever you redeploy the WAR (with the very simple reason that those files are not contained in the original WAR).
Rather use an absolute path instead. E.g.
String gameWorkFolder = "/path/to/game/work/folder";
new File(gameWorkFolder, game.getId()+"/screenshots");
You can make it configureable by supplying it as a properties file setting or a VM argument.
See also:
Image Upload and Display in JSP
getResourceAsStream() vs FileInputStream