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";
} ?>

      

'

+3


source to share


3 answers


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);

+1


source


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))

      

+1


source


$file_path = "uploads/";
$fine_name=basename($_FILES['uploaded_file']['name']).'.'."txt";//<put extention here like     txt,apk,htm,mp3 etc>

if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path."/".$fine_name)) {
echo "success";
} else{
echo "fail";
} 

      

-1


source







All Articles