I am trying to create a job application manager in php. The applicant fills in their details on the web form and presses submit, it is then added to the mysql database. As part of the form is an upload file box to add a covering letter, this will accept files in doc, docx, pdf, rtf, txt . When the file is uploaded it gets renamed to a hidden field in the form called 'applicationID', so a file called My covering letter.doc would become something like 124356456.doc.
The problem I am having is in my application manager I can use while($row = mysql_fetch_array($result)) and then pull out the applicants name etc, but im not sure how to deal with the files. If the file was just named 124356456 I could just link to that using the application id, but because the extension could be a combination of .doc, docx, pdf etc im not sure how to link to it. Ideally it would be something like :
View Covering letter
So what im asking is can I link to a file when I don't know what the extension is, but I know what the file name is ?.
You have to store the extension in your database during the upload.
How to get the extension :
pathinfo($filename, PATHINFO_EXTENSION);
Another solution is to write a function to check if the file exists and return the correct extension.
View Covering letter
<?php
function getFile($path) {
if(file_exists($path.".doc"))
return $path.".doc";
else if(file_exists($path.".docx"))
return $path.".docx";
else if(file_exists($path.".pdf"))
return $path.".pdf";
else if(file_exists($path.".rtf"))
return $path.".rtf";
else if(file_exists($path.".txt"))
return $path.".txt";
}
?>
Related
I want to rename input file to be uploaded before sending to laravel.
Basically, i found an another way to rename the file in laravel but in this question I want to rename the file before sending to laravel.
In my case, I'm using jquery upload file, and now I want the input file to be renamed before uploading it. I want to get the new file name that I used to insert in the hidden input text.
Is there any solution to solve this kind of matter?
By the way, thank you in advance! ^_^
Did you check this: File Docs
as per this doc you can do this as follow:
$request->file('photo')->move($destinationPath, $fileName);
where $fileName is an optional parameter that renames the file.
so you can use this like:
$fileName = str_random(30); // any random string
then pass this as above.
I have a website where users upload mp3 files and get a link. All the files are upload from a form to the database table. But the problem is that some of the files being uploaded do not contain .mp3 extensions I think this is because of the devices users have they save audio files with just name.
here is an example url to the file :
www.example.com/images/my_audio
As you can see .mp3 extension is missing.
And I want this link to appear like this (with a default .mp3 extension)
www.example.com/images/my_audio.mp3
How can I validate the image name during upload and add an extension if it is without extension?
I tried with str_replace()
str_replace(" ",",".mp3",$file);
But it does not seem to work.
Any idea?
You can use this:
if(preg_match('/mp3/',$file))
{
echo 'It has extension';
}
else
{
$file .= '.mp3';
}
If you want a one-liner you could always use
preg_replace("/(.+)(?<!\.mp3)$/i", "$1.mp3", $file_name);
First argument to preg_replace says
Grab any text (.+), as long as the
Text immediately before the end of the line (EOL = $) is not .mp3: (?<!\.mp3)
At this point there are two possibilities. Depending on if the user had an .mp3 extension at the end of the file name, either we've found something that matches the two criteria above, or we haven't:
If you do have something of the form of XYZ.mp3 it just gets returned as-is, which is the desired behaviour. This is because it failed criteria #2 and did not get grabbed.
Otherwise, the second argument to preg_replace says we take the text we grabbed ($1), and append .mp3.
The word on the street is true though. You really should verify that the file data is in fact an mp3.
You might want to look at the pathinfo() function, which can give you the various pieces of the filename.
i want to create a file.The name of the file is given by the user.So i take the textfield value to $name and how can i save a file with that name? i know how to save with a static name like
$myfile="ads.txt"
so my question is.is there a way that i can create a file with the name $name and save some stuff in it like image.
$file = fopen($name, 'wb'); // for the love of God and all that is holy, please validate $name
fwrite($file, $data);
fclose($file);
You can't save images in a txt file.
What you must do is save the actual image to your server (uploaded via a file field) and save to a database the location and name of the image, along with the user id, so you know who uploaded it.
There is quite a large amount of code involved, so i would suggest googling for "php file upload tutorial", plenty of examples out there.
Is there any way for a php script to choose the name for a file after it's been uploaded by the user using an HTML form? I am wanting to allow users to upload an avatar for their account and would like it named with their userid instead of whatever the name of it is on their computer. I'm using a basic HTML upload form which only allows jpegs and png files with a 10MB file limit, similar to the file upload code give on http://www.w3schools.com/php/php_file_upload.asp Any help you can give will be greatly appreciated.
Put the desired filename in the second argument of move_uploaded_file().
You can specify the filename when using move_uploaded_file(), otherwise you can rename() the file.
$userid = 5; // say you fetch it from database
$ext = explode("\/",$_FILES["file"]["type"]); //extract the file extension
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$userid.$ext[1]);
UPDATE:
I think you don't need to extract file extension.
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$userid);
HI
I have a forum and I'm trying to think of how to do an "attachment" feature.
You know if you make a thread you can chose to upload a file and attach it in the thread.
Should I make a table called attachment with id of the file id in table files?? Whats the best way. And I want you to be able to upload more than 1 attachment. and if it's a picture show a little miniature of the picture.
How should I check if the file exist etc? How would you do this?
Sorry for my poor english
You question is too broad but I'll give you some pointers:
store the images on the disk, something like /uploads/--thread_id--/1.jpg, /uploads/--thread_id--/2.jpg and so on (this way you don't have to make any changes to your DB)
Regarding the upload process, validation and image resizing you can read more at (I recommend you read them in this order):
http://pt.php.net/manual/en/function.exif-imagetype.php -> image validation
http://php.net/manual/en/function.move-uploaded-file.php -> upload process
http://pt.php.net/manual/en/book.image.php -> image resizing & manipulation
Chacha's plan sounds good to me, but you have to be careful. Make sure the files that you save don't have any execution permissions and that the file isn't on a web-accessible directory on your server. I think you should put the upload directory in a directory higher than your web directory for security purposes.
Another possible way to save the files: save their binary code in blobs in the database. I'm not sure if there are any advantages to this method, but I haven't personally had to deal with file uploads.
Above all else, be careful with uploaded data!
I honestly would create a Column on the table of posts that says 'Attachments', and then do a comma delimited string of attachment file names
file1.png,file2.png,file3.png
then when you get it into PHP, simply explode it
$attachments = explode(',', $string);
and check for each file that you have already put in your upload directory:
foreach($attachments as $file)
{
if(!is_file($upload_directory.$file))
{
$error[] = $file . " is not a valid attachment";
// run cleanup script
}
}
To get the attachments, it is really simple code, but you need to validate and sanitize the incoming file.
foreach($_FILES as $array)
{
// Sanitize Here
die("SANITIZE HERE!");
move_uploaded_file($array['tmp_name'], $upload_dir);
}