在PHP上传到数据库之前,将URL附加到filename

时间:2015-07-25 15:17:51

标签: php string

我尝试在将图像文件名上传到数据库之前将其添加到图像文件名中。似乎没有任何成效。搜索了Google和Stackoverflow。我想我错过了一些东西。这是我的代码,请找到评论'这是问题'

我有一个变量$ value,它从数据库中提取最新的ID。 我所要做的就是添加变量的路径:" http://example.com/location/something/"

所需输出:图片应重命名为http://example.com/location/something/1.jpg

<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{    
  $title = $_POST['title'];

$file = $_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];

$info = pathinfo($_FILES['file']['name']);
$ext = $info['extension']; // get the extension of the file
$folder="uploads/";
function execute_scalar($sql,$def="") {
$rs = mysql_query($sql) or die("bad query");
if (mysql_num_rows($rs)) {
    $r = mysql_fetch_row($rs);
    mysql_free_result($rs);
    return $r[0];
    mysql_free_result($rs);
}
return $def;
}

$value = execute_scalar("select max(ID)+1 from tablename");


$newname = "$value.".$ext; // Here is the issue.
$newname = "http://example.com/location/something/"."$value.".$ext; //did not  work
$newname = "http://example.com/location/something/ $value.".$ext; //did not  work
// even tried assingning url to variable still did not worked

if(move_uploaded_file($file_loc,$folder.$newname))
{
$sql="INSERT INTO   tablename(id,title,image)VALUES('$value','$title','$newname')";
   mysql_query($sql);
 ?>
<script>
alert('successfully uploaded');
    window.location.href='index.php?success';
    </script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
    window.location.href='index.php?fail';
    </script>
<?php
}
}
?>

是URL吗?

3 个答案:

答案 0 :(得分:0)

尝试移动您的报价,如下所示:

$ newname =&#34; http://example.com/location/something/&#34;。 $ value。&#34;。&#34; 。 $ EXT;

答案 1 :(得分:0)

问题是您不能在文件名中使用 / (斜杠),因为它充当目录路径。

答案 2 :(得分:0)

我找到了解决方案:

$newname =  "http:\\//www.example.com\/location\/something\/\/$value.".$ext;

工作正常:D