函数saveImagePath
没有'当它在主函数uploadFile
内时工作。但是文件成功上传。上传没有问题。它是关于这个嵌套函数的全部内容。当我执行以下代码时,除了这个`saveImagePath'功能
为了验证这一点,我编写了导致主函数外部嵌套函数的问题,并使用$this->saveImagePath
和Lo调用它!有效。这背后的问题是什么?
是否与return语句有关?
function uploadFile($fields){
$files = $_FILES['photo'];
$count = count($files['name']);
if($fields['type'] == "PROFILEPIC"){
$folderName="userimages/";
}else{
$folderName="personalmarkerimages/";
$transportStopId=$fields['transportStopId'];
}
function saveImagePath($imagePath, $transportStopId)
{
$db=$this->dbConnect();
$mySqlQuery = "UPDATE mytablename SET imagePath=:imagePath WHERE filedname=:stopId";
$stmt=$db->prepare($mySqlQuery);
$stmt->bindParam("imagePath", $imagePath);
$stmt->bindParam("stopId", $transportStopId);
$stmt->execute();
}
if(gettype($files['name'])=='array'){
$num=0;
for($i = 0 ; $i < $count ; $i++) {
if ($files['error'][$i] === 0) {
$target_file='/myaddress/' . $folderName . $files['name'][$i];
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo '{"status": "error", "message": "Not a valid format"}';
}else{
if (file_exists($target_file)) {
echo '{"status": "error", "message": "File already exist"}';
break;
}else if(move_uploaded_file($files['tmp_name'][$i], $target_file) === true) {
saveImagePath($target_file, $transportStopId);
$num=$i+1;
}
else{
}
}
}
}
}else{
echo '{"status": "error", "message": "Couldn\'t upload"}';
}
if($num==$count){
echo '{"status": "success", "values": "'.$fields['description'].'"}';
}
}
答案 0 :(得分:0)
函数中的函数看起来像是一个类,所以我已经将代码更改为类:
class FileUploader {
public function uploadFile($fields){
$files = $_FILES[ 'photo' ];
$count = count( $files[ 'name' ] );
if ( $fields[ 'type' ] == "PROFILEPIC" ) {
$folderName = "userimages/";
} else {
$folderName = "personalmarkerimages/";
$transportStopId = $fields[ 'transportStopId' ];
}
if(gettype($files['name'])=='array'){
$num=0;
for($i = 0 ; $i < $count ; $i++) {
if ($files['error'][$i] === 0) {
$target_file='/myaddress/' . $folderName . $files['name'][$i];
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
echo '{"status": "error", "message": "Not a valid format"}';
}else{
if (file_exists($target_file)) {
echo '{"status": "error", "message": "File already exist"}';
break;
}else if(move_uploaded_file($files['tmp_name'][$i], $target_file) === true) {
$this->saveImagePath($target_file, $transportStopId);
$num=$i+1;
}
else{
}
}
}
}
}else{
echo '{"status": "error", "message": "Couldn\'t upload"}';
}
if($num==$count){
echo '{"status": "success", "values": "'.$fields['description'].'"}';
}
}
protected function saveImagePath($imagePath, $transportStopId)
{
$db=$this->dbConnect();
$mySqlQuery = "UPDATE mytablename SET imagePath=:imagePath WHERE filedname=:stopId";
$stmt=$db->prepare($mySqlQuery);
$stmt->bindParam("imagePath", $imagePath);
$stmt->bindParam("stopId", $transportStopId);
$stmt->execute();
}
}
现在您可以使用它:
(new FileUploader())->uploadFile($fields);