PHP文件上传和分配目录

时间:2011-08-11 21:25:06

标签: php

很抱歉,我很难解决问题。我想要做的是上传文件,然后传递目录,但它的nbot工作方式我期望。上传到服务器到一个名为'uploads'的文件,但我无法传递目录,因为我需要的,在后面的解码函数中: 以下是使用以下内容上传文件的代码:

 <?php
 // Where the file is going to be placed 
 $target_path = "uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
{
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
 } else{
    echo "There was an error uploading the file, please try again!";
 }

?>

然后我想在$ json = $ api-&gt; decode()之后将目录传递到此处:它位于我放置的括号之间($ target_path .basename($ _FILES ['uploadedfile'] ['name] ']))但它不起作用,我不知道如何解决它。案例解码应该是刚刚上传的文件的目录,然后解码里面的内容:

  case "decode":
    $json = $api->decode ($target_path .   basename( $_FILES['uploadedfile']  ['name']));
    $start='{"content":"';
    $pos_start = strpos($json, $start);

    $end='"}';
    $pos_end = strpos($json, $end);

thanx任何建议,请帮助我,如果你知道如何,thanx

2 个答案:

答案 0 :(得分:0)

构建JSON的方式看起来有缺陷。通常,最好先累积数据,然后在最后一步执行json_encode。这样你的JSON永远不会被意外损坏。它可能仍然没有语义上的有效,但至少你可以看到原因;)

$data = array(
    "content" => $target_path . basename( $_FILES['uploadedfile']['name'])
);
return json_encode($data);

答案 1 :(得分:0)

我希望这会有所帮助:

$json = $api->decode ($target_path .   basename( $_FILES['uploadedfile']  ['name']));
$o=json_decode($json);
echo $o->content;

如果不起作用,请尝试:

$json = $api->decode ($target_path);
$o=json_decode($json);
echo $o->content;

由于您使用的是上面的$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);