Search code examples
phpuploadcare

uncaught exception: could not open file


so i'm trying to upload to uploadcare cdn and i get this error: Uncaught Exception: couldn't open file "ce3577f708f527ff570e0d21acb39c62.jpg"

$description = $_POST['uploaddesc'];
  list($width,$height) = getimagesize($_FILES['uploadimage']['tmp_name']);
 $extensions  = array('image/jpeg','image/jpg','image/gif', 'image/png', 'image/tiff');
 $move = 'uploads/';
 $finfo = finfo_open(FILEINFO_MIME_TYPE);


$extension = pathinfo($_FILES['uploadimage']['name']);
if(!$_FILES['uploadimage']['tmp_name']){
$errors[]="Please choose an image";
}
if(empty($description)){
    $errors[]='Please choose a description for the image';
}
//IF IMAGE IS UPLOADED..
if($_FILES['uploadimage']['tmp_name']){
    $mime = finfo_file($finfo, $_FILES['uploadimage']['tmp_name']);

// IF MIME FAILES OR GETIMAGESIZE IS NULL (meaning its no an image) if(!getimagesize($_FILES['uploadimage']['tmp_name']) && !in_array($_FILES['uploadimage']['tmp_name'],$mime)){ $errors[]='Please choose an image'; unset($_FILES['uploadimage']['tmp_name']); }

     if(empty($errors)){

 $randString = md5(time()); //encode the timestamp - returns a 32 chars long string
 $fileName = $_FILES["uploadimage"]["name"]; //the original file name
 $splitName = explode(".", $fileName); //split the file name by the dot
 $fileExt = end($splitName); //get the file extension
 $newFileName  = strtolower($randString.'.'.$fileExt); //join file name and ext.
 if(move_uploaded_file($_FILES['uploadimage']['tmp_name'], $move . $newFileName)){
 $description = $_POST['uploaddesc']; 
 $user->insertArt($description, 'uploads/' . $newFileName, $user->getUsername($_SESSION['username']));
 $file = $api->uploader->fromPath('uploads/' . $newFileName);
 $file->store();    

//throw new ErrorException('MEMORY USAGE ' . memory_get_peak_usage());


 }
     }

}

Solution

  • i have fixed it. i had to use realpath('uploads/' . $newfilename);