...

View Full Version : Can some assist with url issue: spaces are converted to "%2F"



Daniellez
10-09-2009, 09:04 PM
Here's the filename code:


filename=" . htmlentities(rawurlencode($file['filename'])) . "

Is there a way to modify this to deal with spaces in a filename?

Should the spaces be removed when the file is uploaded? Here's the upload code:


if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
if($_REQUEST['folder'] != './')
$targetPath = $_SERVER['DOCUMENT_ROOT'] .'/'.$_REQUEST['folder'];
else
$targetPath = $_SERVER['DOCUMENT_ROOT'] .'/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];

if(!file_exists($targetPath))
mkdir(str_replace('//','/',$targetPath), 0755, true);

move_uploaded_file($tempFile,$targetFile);
echo "1";

V@no
10-10-2009, 03:24 AM
you could try replace spaces with a "_" or something...
and also, if you are going to modify the original filenames, then maybe you should also ensure that file with the same name doesn't exist:

if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
if($_REQUEST['folder'] != './')
$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/' . $_REQUEST['folder'] . '/';
else
$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/';

$targetPath = str_replace('//','/',$targetPath);

if(!file_exists($targetPath))
mkdir($targetPath, 0755, true);

$_FILES['Filedata']['name'] = preg_replace("/[^-\._a-zA-Z0-9]/", "_", trim($_FILES['Filedata']['name'])); //replace any characters not in the list with a _

$targetFile = $targetPath . $_FILES['Filedata']['name'];

if (preg_match("/(.*)\.(.*)/", $_FILES['Filedata']['name'], $data))
{
$name = trim($data[1]);
$ext = trim($data[2]);
}
else
{
$name = $_FILES['Filedata']['name'];
$ext = "";
}

if ($ext !== "")
$ext = "." . $ext;

$suf = "";
$i = 1;
while(file_exists($targetFile = $targetPath . $name . $suf . $ext)) //if file exists append sufix and check again. repeat until file doesn't exist.
$suf = "_" . $i++;

move_uploaded_file($tempFile, $targetFile);
echo "1";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum