move_uploaded_file example


  public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, Settings $settings, LoggerInterface $logger) {
    $this->streamWrapperManager = $stream_wrapper_manager;
    $this->settings = $settings;
    $this->logger = $logger;
  }

  /** * {@inheritdoc} */
  public function moveUploadedFile($filename$uri) {
    $result = @move_uploaded_file($filename$uri);
    // PHP's move_uploaded_file() does not properly support streams if     // open_basedir is enabled so if the move failed, try finding a real path     // and retry the move operation.     if (!$result) {
      if ($realpath = $this->realpath($uri)) {
        $result = move_uploaded_file($filename$realpath);
      }
      else {
        $result = move_uploaded_file($filename$uri);
      }
    }

    
throw HTTPException::forAlreadyMoved();
        }

        if ($this->isValid()) {
            throw HTTPException::forInvalidFile();
        }

        $name ??= $this->getName();
        $destination = $overwrite ? $targetPath . $name : $this->getDestination($targetPath . $name);

        try {
            $this->hasMoved = move_uploaded_file($this->path, $destination);
        } catch (Exception $e) {
            $error   = error_get_last();
            $message = strip_tags($error['message'] ?? '');

            throw HTTPException::forMoveFailed(basename($this->path)$targetPath$message);
        }

        if ($this->hasMoved === false) {
            $message = 'move_uploaded_file() returned false';

            throw HTTPException::forMoveFailed(basename($this->path)$targetPath$message);
        }
$filePath = tempnam($destPath, 'snippets_');
        if ($filePath === false) {
            echo json_encode([
                'success' => false,
                'message' => sprintf('Could not create a tmp file for %s', $filePath),
            ]);

            return;
        }

        if (move_uploaded_file($_FILES['file']['tmp_name']$filePath) === false) {
            echo json_encode([
                'success' => false,
                'message' => sprintf('Could not move %s to %s.', $_FILES['file']['tmp_name']$filePath),
            ]);

            return;
        }

        $this->uploadedFilePath = $filePath;
        chmod($filePath, 0644);

        
$this->validateActive();

        if (false === $this->isStringNotEmpty($targetPath)) {
            throw new InvalidArgumentException(
                'Invalid path provided for move operation; must be a non-empty string'
            );
        }

        if ($this->file) {
            $this->moved = PHP_SAPI === 'cli'
                ? rename($this->file, $targetPath)
                : move_uploaded_file($this->file, $targetPath);
        } else {
            Utils::copyToStream(
                $this->getStream(),
                new LazyOpenStream($targetPath, 'w')
            );

            $this->moved = true;
        }

        if (false === $this->moved) {
            throw new RuntimeException(
                
public function move(string $directory, string $name = null): File
    {
        if ($this->isValid()) {
            if ($this->test) {
                return parent::move($directory$name);
            }

            $target = $this->getTargetFile($directory$name);

            set_error_handler(function D$type$msg) use (&$error) { $error = $msg});
            try {
                $moved = move_uploaded_file($this->getPathname()$target);
            } finally {
                restore_error_handler();
            }
            if (!$moved) {
                throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname()$targetstrip_tags($error)));
            }

            @chmod($target, 0666 & ~umask());

            return $target;
        }

        
public function move(string $directory, string $name = null): File
    {
        if ($this->isValid()) {
            if ($this->test) {
                return parent::move($directory$name);
            }

            $target = $this->getTargetFile($directory$name);

            set_error_handler(function D$type$msg) use (&$error) { $error = $msg});
            try {
                $moved = move_uploaded_file($this->getPathname()$target);
            } finally {
                restore_error_handler();
            }
            if (!$moved) {
                throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname()$targetstrip_tags($error)));
            }

            @chmod($target, 0666 & ~umask());

            return $target;
        }

        

    $move_new_file = apply_filters( 'pre_move_uploaded_file', null, $file$new_file$type );

    if ( null === $move_new_file ) {
        if ( 'wp_handle_upload' === $action ) {
            $move_new_file = @move_uploaded_file( $file['tmp_name']$new_file );
        } else {
            // Use copy and unlink because rename breaks streams.             // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged             $move_new_file = @copy( $file['tmp_name']$new_file );
            unlink( $file['tmp_name'] );
        }

        if ( false === $move_new_file ) {
            if ( str_starts_with( $uploads['basedir'], ABSPATH ) ) {
                $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
            } else {
                
Home | Imprint | This part of the site doesn't use cookies.