No file extension
I am using my android app to upload some files to my server. The files are successfully uploaded to the server, but there is no extension for files that have a [Space] in the filename. Every time I try to open these files, the system prompts me to select an application to open. If I select the appropriate application, the file opens successfully. Is there a way to make these extensions available so I don't have to select an app every time?
My PHP code:
$file_path = "uploads/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
echo "success";
} else{
echo "fail";
} ?>
'
source to share
I have updated my Java code to the following:
Previous Code
dos.writeBytes("Content-Disposition: form-data; name= \"uploaded_file\";filename="+ fileName + "" + lineEnd);
Updated code
dos.writeBytes("Content-Disposition: form-data; name= \"uploaded_file\";filename="+ fileName.replace(" ", "_") + "" + lineEnd);
source to share
You can use str_replace()
to replace spaces with an underscore:
$filename = str_replace(' ', '_', $filename);
or using preg_replace()
$file_path = "uploads";
$file = basename($_FILES['uploaded_file']['name']);
$newfilename = preg_replace('/\s+/', '_', $file);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path. "/" .$newfilename))
source to share