Avatar billede simon_gs_13 Nybegynder
05. februar 2006 - 19:38 Der er 6 kommentarer og
1 løsning

fjerne mellem i fil ved upload

I relation til http://www.eksperten.dk/spm/685381 så har jeg et upload script. Men nu vil jeg gerne have det script til at fjerne mellemrum i den fil som bliver uploadet, da filtræet ikke kan klare mellemrum.

Altså uploadscriptet skal lave "fil navn.fil" om til "filnavn.fil" ved upload.

--- upload script ----

<!-- put all in upload.php -->
<form action="upload.php" method="post" enctype="multipart/form-data">
<span class="style7 style8">Browse a File to Upload: <i>MAXIMUM filesize 10MB.</i></span><br>
<input type="file" name="filetoupload"><br>
<input type="hidden" name="MAX_FILE_SIZE" value="<?echo $size_bytes; ?>">
<br>
<input type="Submit" value="Upload File"><br>
</form>

<?php

/*  Description -----------------------------------------------------
The Super Global Variable $_FILES is used in PHP 4.x.x.
$_FILES['upload']['size'] ==> Get the Size of the File in Bytes.
$_FILES['upload']['tmp_name'] ==> Returns the Temporary Name of the File.
$_FILES['upload']['name'] ==> Returns the Actual Name of the File.
$_FILES['upload']['type'] ==> Returns the Type of the File.

So if I filetoupload the file 'test.doc', the $_FILES['upload']['name']
would be 'phptut.doc' and $_FILES['upload']['type'] would be 'application/msword'.
---------------------------------------------------------------------*/
// this is the upload dir where files will go.
//Don't remove the /
//Chmod it (777)
$upload_dir = "images/";  //change to whatever you want.
// files less than 10MB
$size_bytes = 10485760; //bytes  will be uploaded
//check if the directory exist or not.
if (!is_dir("$upload_dir")) {
    die ("The directory <b>($upload_dir)</b> doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
        die ("The directory <b>($upload_dir)</b> is NOT writable, Please Chmod (777)");
}

//Check first if a file has been selected
//is_filetoupload_file('filename') returns true if
//a file was filetoupload via HTTP POST. Returns false otherwise.
if (is_uploaded_file($_FILES['filetoupload']['tmp_name']))
{

//Get the Size of the File
$size = $_FILES['filetoupload']['size'];
//Make sure that $size is less than 10MB (10000000 bytes)
if ($size > $size_bytes)
{
echo "File Too Large. Please try again.";
exit();

}
// $filename will hold the value of the file name submetted from the form.
$filename =  $_FILES['filetoupload']['name'];
// Check if file is Already EXISTS.
if(file_exists($upload_dir.$filename)){
  echo "<span class='style7 style8'>Oops! The file named <b>$filename </b>already exists </span>";
  exit();
}

//Move the File to the Directory of your choice
//move_filetoupload_file('filename','destination') Moves an filetoupload file to a new location.
if (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename)) {

//tell the user that the file has been filetoupload
echo "<span class='style7 style8'>File (<a href=$upload_dir$filename>$filename</a>) uploaded!</span>";
exit();

}
else
{
//Print error
echo "There was a problem moving your file";
exit();

}
}

?>

/ Simon
Avatar billede coderdk Praktikant
05. februar 2006 - 20:16 #1
if (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename)) {

laves om til:

if (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.str_replace(" ", "", $filename))) {
Avatar billede nielle Nybegynder
05. februar 2006 - 20:17 #2
Prøv at ændre dette:

// $filename will hold the value of the file name submetted from the form.
$filename =  $_FILES['filetoupload']['name'];

- til:

// $filename will hold the value of the file name submetted from the form.
$filename = $_FILES['filetoupload']['name'];
$filename = preg_replace("/\s/", "", $filename);
Avatar billede simon_gs_13 Nybegynder
06. februar 2006 - 10:44 #3
Takker, begge to. Begge modeller virker så det må være først til mølle princippet. Så coderdk smid et svar.

/ simon
Avatar billede coderdk Praktikant
06. februar 2006 - 11:39 #4
Ok :)
Avatar billede nielle Nybegynder
06. februar 2006 - 17:36 #5
coderdk er mere end velkommen til pointene :^)

Blot vil jeg dog påpege at med den løsning, skal du huske at rette alle de stedet hvor $filename bruges. Med min løsning skal der kun rettes et sted. Oven i det kommer at (synes jeg i hvert fald selv) at min løsning giver kode som er lettere at læse (og dermed også at vedligeholde).

Skulle jeg lave noget om, ville jeg dog foretrække at benytte str_replace() frem for preg_replace(). ;^)
Avatar billede coderdk Praktikant
06. februar 2006 - 19:27 #6
heheh ja, nielle's er faktisk bedre - jeg skimmede blot koden hurtigt og fandt move_uploaded_file ;) nielle, du er mere end velkommen til at få pointene, hvis du vil :)
Avatar billede nielle Nybegynder
06. februar 2006 - 19:31 #7
Nej tak. Men ellers tak for tilbudet. :^)
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester



IT-JOB

Unik System Design A/S

Driftskonsulent for Hostingcenter

Udviklings- og Forenklingsstyrelsen

Kontorchef med ansvar for tværgående service management

MAN Energy Solutions

Cloud FinOps Practitioner

Udviklings- og Forenklingsstyrelsen

IT-generalister til rollen som systemejer

Gehl Architects ApS

IT Supporter