2013-02-11 57 views
-1

我有一个网站,允许用户上传文件。当在另一个页面上载文件时,用户可以下载该文件。问题是如果文件名中有一个空格,则只会选取第一个作业而不是整个文件名。我想知道是否有一种方法可以下载带有空格的文件,或者一个简单的选项可能是将下划线添加到文件名以便下载。用Underscore替换空格,以便我可以下载文件。

//This is the directory where images will be saved 
$target = "../images/avatars/"; 
$target = $target . basename($_FILES['photoLocation']['name']); 

// Check to see all fields have been completed 
$photoLocation =($_FILES['photoLocation']['name']); 

if (!empty($photoLocation)) 
{ 

    // Create an SQL query to add the comment 
    $sql = "INSERT INTO tblPhotoUpload (photoLocation) VALUES ('$photoLocation')"; 



    // Connect to the database 
    connect(); 

    // Run the query and store the result in a variable 
    $result = mysql_query($sql) or die("Could not run query"); 


    //Writes the photo to the server 
    if(move_uploaded_file($_FILES['photoLocation']['tmp_name'], $target)) 

    // Close connection to the database 
    mysql_close() 

而我正在显示这样的文件。

while($record = mysql_fetch_array($result)) 
{ 
?> 

<tr class="row"> 
    <td class="cell"><a href= http://xxxxxxxxxxxxxxxxxxxxxx.com/images/avatars/<?php   echo $record["photoLocation"];?>>Download</a> </td> 
+0

感谢大家在这么快的时间回复。我已经找到了答案,我已经打勾了。 很多你说过一些很棒的东西给我考虑。 谢谢你们 – user1775454 2013-02-11 13:42:54

回答

-2

str_replace()

$文件= str_replace函数(””, '_',$文件);

或在您的情况:

$target = $target . basename(str_replace(' ','_',$_FILES['photoLocation']['name'])); 

$photoLocation =(str_replace(' ','_',$_FILES['photoLocation']['name'])); 
+0

感谢您的回复,我的代码应该放在哪里? – user1775454 2013-02-11 13:28:26

+0

看到编辑答案 – vodich 2013-02-11 13:35:47

+0

接受答案和-1 :) – vodich 2013-02-12 07:14:10

2

你正在处理一个转义方案的逃脱方案中,当你使用网址。特别是,你有逃跑的网址,以便一个URL,然后你要逃避URL作为对HTML嵌入它:

$url = "http://somesite.tld/some/path/" . urlencode($filename); 


echo '<a href="' . htmlspecialchars($url) . '">link</a>'; 

这实在是太该死很难想象的(现实的)情况这实际上需要htmlspecialchars,但wooo偏执狂!

哦,而且,继续前进并引用所有属性值通常是个好主意。

当你这样做:<tag attr=value with spaces>浏览器解释withspaces作为附加属性,attr


如果你这样做,虽然走的路线替代的价值NOTAS一部分,你只是在寻找一个简单的str_replace函数电话:

$val = str_replace(' ', '_', $replaceMe); 
+0

感谢您的答复,替换方法会很好,只是在我的代码中约需要我吗? – user1775454 2013-02-11 13:29:23

+0

@ user1775454无论何时您处理文件名:所以当保存上传然后重新下载它时。 – Corbin 2013-02-11 13:32:45

0

还没有测试,但这种逻辑似乎是正确的。

$name = str_replace(' ', '_', $photoLocation); 
0

我会建议不要显示图像的原始名称,因为有很多与此相关的安全问题。

你可以这样做的一种方式是编码文件名并将其保存在数据库的额外字段中。

例如

$sql = "INSERT INTO tblPhotoUpload (photoLocation,photourl) VALUES ('$photoLocation','".md5($photoLocation)."')"; 

当显示器使用phtolocation的URL并显示路由到照片的位置。

祝你好运,如果你需要完整的例子让我知道

相关问题