public function actionUpload() { include __DIR__ . "./../uploader/Uploader.php"; $upload_dir = \Yii::$app->getModule("file")->getStorageDir(); $uploader = new \FileUpload('uploadfile'); $fileModel = new File(); $realName = $_GET["uploadfile"]; // Handle the upload $isUplaoded = $uploader->handleUpload($upload_dir); $result = false; $errorMsg = ''; if ($isUplaoded) { $fileModel = new File(); $fileModel->setAttribute("real_name", $realName); $fileModel->setAttribute("name_on_server", $uploader->getSavedFileName()); $fileModel->setAttribute("size", $uploader->getFileSize()); if (!$fileModel->save()) { $uploader->rollBack(); $errorMsg = "Entity save error"; } else { if (!empty($_GET["relateTo"])) { if (!$fileModel->linkWith($_GET["relateTo"])) { $uploader->rollBack(); $fileModel->delete(); $errorMsg = "Entity link error"; } else { $result = true; } } else { $result = true; } } } else { $errorMsg = $uploader->getErrorMsg(); } if ($result) { echo json_encode(array('success' => true)); } else { exit(json_encode(['success' => false, 'msg' => $errorMsg])); } }
$upload_name = 'file_' . date("Y-m-d_His."); if (isset($_GET['file_tree'])) { include dirname(__FILE__) . "/extras/php_file_tree.php"; die(php_file_tree($upload_dir, "javascript:shoWImg('[link]',[id])", $allowed_extensions)); } if (isset($_GET['uploadfile'])) { require dirname(__FILE__) . '/extras/Uploader.php'; $Upload = new FileUpload('uploadfile'); $ext = $Upload->getExtension(); // Get the extension of the uploaded file $Upload->newFileName = $upload_name . $ext; $result = $Upload->handleUpload($upload_dir, $allowed_extensions); if (!$result) { die(json_encode(array('success' => false, 'msg' => $Upload->getErrorMsg()))); } else { die(json_encode(array('success' => true, 'FileName' => $Upload->getFileName(), 'Size' => $Upload->getFileSize(), 'SavedFile' => $Upload->getSavedFile(), 'Extension' => $Upload->getExtension()))); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>تحميل الملفات</title> <link href="./assets/css/bootstrap.min.css" rel="stylesheet"> <link href="./assets/css/bootstrap-rtl.min.css" rel="stylesheet"> <link href="./assets/css/styles.css" rel="stylesheet"> <script src="./assets/js/jquery.min.js"></script>
public function testMoveFile() { $tmpPath = realpath(__DIR__ . '/Sample') . '/upload.tmp'; $targetPath = realpath(__DIR__ . '/Sample') . '/upload_moved.txt'; $targetPath2 = realpath(__DIR__ . '/Sample') . '/upload_moved_xtracopy.txt'; if (file_exists($tmpPath)) { if (!unlink($tmpPath)) { $this->markTestSkipped('Temp upload file remains from old test and could not be removed'); return; } } file_put_contents($tmpPath, 'TESTING_123'); if (file_exists($targetPath)) { if (!unlink($targetPath)) { $this->markTestSkipped('Moved upload file remains from old test and could not be removed'); return; } } if (file_exists($targetPath2)) { if (!unlink($targetPath2)) { $this->markTestSkipped('Moved upload file (2) remains from old test and could not be removed'); return; } } $expectedSize = filesize($tmpPath); $file = new FileUpload(); $file->setTemporaryPath($tmpPath); // Initial copy $this->assertTrue($file->saveTo($targetPath)); // Ensure file is copied OK and temp path is gone $this->assertFileExists($targetPath); $this->assertFileNotExists($tmpPath); $this->assertTrue(filesize($targetPath) == $expectedSize); // Ensure that the file size and current path reported are accurate $this->assertEquals($expectedSize, $file->getFileSize()); $this->assertEquals($targetPath, $file->getCurrentPath()); // Copy the file again to a second location $this->assertTrue($file->saveTo($targetPath2)); $this->assertTrue(filesize($targetPath2) == $expectedSize); $this->assertFileExists($targetPath); $this->assertFileExists($targetPath2); $this->assertFileNotExists($tmpPath); $this->assertEquals($expectedSize, $file->getFileSize()); $this->assertEquals($targetPath2, $file->getCurrentPath()); // cleanup @unlink($tmpPath); @unlink($targetPath); @unlink($targetPath2); }