My image upload doesn't work:
Controller:
if (Input::hasFile('image')) {
$bikecreate->image = Input::file('image');
$destinationPath = public_path().'/upload/';
$filename = str_random(6) . '_' . $bikecreate->users_id ;
Input::file('image')->move($destinationPath, $filename);
}
Form:
{{ Form::file('image', array('files' => true)) }}
After accepting form everything looks ok, but after the end of upload, filepath in database show .tmp/file at my server.
Without seeing the rest of your code it's hard to see exactly what's going on but my guess is that your line $bikecreate->image = Input::file('image') is where you're setting the file's path for the database. You've actually set the UploadedFile instance as the image property on $bikecreate there, which, presumably, when serialised to something to put into the database gets __toString() called on it.
__toString() called on a File instance (which itself inherits __toString from SPLFileInfo returns the path to that file. So you'd think you're get the uploaded filename, but actually because an uploaded file is actually a temporary file in PHP, you get the temporary name.
Try changing that line to the following:
$bikecreate->image = Input::file('image')->getClientOriginalName();
This retrieves the actual original name of the uploaded file, not the temporary path given to it by PHP.
It goes without saying that this is only pertinent to UploadedFiles, normal files should just be able to be __toStringed to get the path to the file, although you'll notice that it would be the full path and not the basename. To get that, use getBaseName().
Related
Octokit allows me to easily retrieve the list of files in a given folder like so:
var directoryContent = await _githubClient.Repository.Content.GetAllContents("MyUsername", "MyProject", "TheFolderName").ConfigureAwait(false);
Unfortunately, the Content property for each file is null which means that I have to loop through the list of files and retrieve each one separately like so:
foreach (var file in directoryContent)
{
var fileWithContent = await _githubClient.Repository.Content.GetAllContents("MyUsername", "MyProject", file.Path).ConfigureAwait(false);
... do something with the content ...
}
Is there a more efficient way to get the content of all files in a given folder?
Just for reference: This is on purpose. If you read a whole directory and load all the content in one step it's huge load. So, the first call skips the content (Content property is null). The second step requests just this file and then the content is present.
Only thing I'm still missing is a method that can return just one result instead of a list if I pull a single file.
I am a newbie to CRM. Trying to create an email with an attachment. Got the email creation part working but facing a hard time in attaching the file that I want to attach from the file explorer.
The activitymimeattachment entity has an attribute called "filename" but that attaches an empty file with the given name rather than the file from file explorer. Tried giving the full path of the file in the filename attribute but that attach only the file with the given name, but not from the explorer.
This is the code I have to attach a file to the email.
Entity attach = new Entity("activitymimeattachment");
attach["filename"] = "Stack.txt";
attach["mimetype"] = "text/plain";
attach["attachmentnumber"] = 1;
attach["objectid"] = new EntityReference("email", emailId);
attach["objecttypecode"] = "email";
service.Create(attach);
Stack.txt file is in my C:/users/name/Files/Stack.txt
How do I specify the file path to the activitymimeattachment attribute?
Any answer to the question would be much helpful.
Thanks in advance.
You need to read in the text and base64 encode it to the body attribute:
attach["body"] = System.Convert.ToBase64String(new ASCIIEncoding().GetBytes(System.IO.File.ReadAllText(#"C:\Temp\alljobs.txt")));
I'm running a code in Maltab the creates directories through mkdir. Problem is, I'm creating their name by some logic on run-time, so I don't know what the dir name would be. I know I can first create the name as
string dirName = nameLogic();
mkdir(dirName);
but I would like to know the dirName from the created directory itself. Naivly, that would be
[outputdirName] = mkdir(fuzzylogicdirName);
I should add that I'm not religiously attached to mkdir, and another yet more suitable method might be in place.
Thanks
I might get you wrong. In any case what mkdir does is just creating a folder, hence the folder name must be known (possibly determined at run-time) before the call.
A structure like
folderName = folderNameLogic([run_time_variables]);
% # folderName = 'something_run_time_variables(1)_and_run_time_variables(2)'
status = mkdir(folderName)
if status == 1
disp(['success in creating folder ' folderName]);
else
disp(['ERROR in creating folder ' folderName]);
end
is thus necessary.
Clearly nothing prevents you from wrapping the call a function of yours returning the folder name. E.g.
function [folderName] = mkdir_retname(folderName)
status = mkdir(folderName);
if status == 0
folderName = '0';
end
end
Can anyone suggest how using the form validation rules I can say the following:-
If no file is uploaded - then create a rule to say 'no file uploaded' using the form validator library.
I am using CodeIgniter 2.
For instance - it is simple to validate on a text input field using the following, but I cannot understand how this is done with upload (that uses the $_FILES array rather than $_POST)
eg.
$this->form_validation->set_rules('title', 'Title', 'required'); // input field named 'title' is required
CodeIgniter's File Uploading class handles its own validation - no need to use the Form Validation class.
Per the documentation,
$this->upload->display_errors()
Retrieves any error messages if the do_upload() function returned
false. The function does not echo automatically, it returns the data
so you can assign it however you need.
In your example above, if the file input is left empty, $this->upload->display_errors() will return the following:
You did not select a file to upload.
So, set your preferences (see the "Setting Preferences" section of the documentation for exactly what is available):
// The following are just examples from the documentation...
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// ... other preferences as necessary
If any of the above fail during the attempted upload, $this->upload->display_errors() will retrieve the appropriate error message. Simply pass it to your view to display the error(s).
Hope that helps.
I have a file reader channel picking up an xml document. By default, a file reader channel populates the 'originalFilename' in the channel map, which ony gives me the name of the file, not the full path. Is there any way to get the full path, withouth having to hard code something?
You can get any of the Source reader properties like this:
var sourceFolder = Packages.com.mirth.connect.server.controllers.ChannelController.getInstance().getDeployedChannelById(channelId).getSourceConnector().getProperties().getProperty('host');
I put it up in the Mirth forums with a list of the other properties you can access
http://www.mirthcorp.com/community/forums/showthread.php?t=2210
You could put the directory in a channel deploy script:
globalChannelMap.put("pickupDirectory", "/Mirth/inbox");
then use that map in both your source connector:
${pickupDirectory}
and in another channel script:
function getFileLastModified(fileName) {
var directory = globalChannelMap.get("pickupDirectory").toString();
var fullPath = directory + "/" + fileName;
var file = Packages.java.io.File(fullPath);
var formatter = new Packages.java.text.SimpleDateFormat("yyyyMMddhhmmss");
formatter.setTimeZone(Packages.java.util.TimeZone.getTimeZone("UTC"));
return formatter.format(file.lastModified());
};
Unfortunately, there is no variable or method for retrieving the file's full path. Of course, you probably already know the path, since you would have had to provide it in the Directory field. I experimented with using the preprocessor to store the path in a channel variable, but the Directory field is unable to reference variables. Thus, you're stuck having to hard code the full path everywhere you need it.