0

I have been trying this code:

$file_tmp = $_FILES['video_imagen']['tmp_name']; list($width, $height) = getimagesize($file_tmp); 

But it gives me an error. I'm not able to fix it

UPDATED

Here you have the PHP code that I have developed. How can I fix it. I have been searching for solutions in different forums...

I get this error

Notice: getimagesize(): Read error! in C:\wamp\www\tapeHD\include\php\database.php on line 282

 public static function SubirVideo($titulo, $video_directorio, $imagen_directorio, $user, $descripcion){ //ID AUTO INCREMENT $sql = "INSERT INTO video(nombre, directorio, imagen, visitas, likes, usuario_id, fecha_subida, descripcion) VALUES ($titulo, $video_directorio, $imagen_directorio, 0, 0, $user, NOW(), $descripcion)"; $resultado = self::Conexion($sql); return $resultado; } public static function SubirVideoErrores(){ $error = ""; if(isset($_POST["boton_upload_video"])){ $uploadImagen = $_FILES['video_imagen']['name']; $tipoImagen = $_FILES['video_imagen']['type']; $uploadVideo = $_FILES['video_file']['name']; $tipoVideo = $_FILES['video_file']['type']; $titulo = $_POST["video_title"]; $descripcion = $_POST["video_description"]; $directorio = $_SERVER['DOCUMENT_ROOT']."/tapeHD/include/database/usuarios/".$_SESSION["usuario"]."/videos"; if($tipoImagen == "image/jpeg"){ $file_tmp = $_FILES['video_imagen']['tmp_name']; list($width, $height) = getimagesize($file_tmp); if($_FILES['video_imagen']['size'] < (1024000)){ if($width != 320 && $height != 180){ $error = "<div id='error_alert'><div class='error_container'><i class='fa fa-times awesome error_close btn'></i><div class='error_text'>La imagen debe de ser 320x180</div></div></div>"; } else { if($tipoVideo == "video/mp4"){ move_uploaded_file($_FILES['video_file']['tmp_name'], $directorio."/".$uploadVideo); move_uploaded_file($_FILES['video_imagen']['tmp_name'], $directorio."/".$uploadImagen); $video_directorio = "include/database/usuarios/".$_SESSION["usuario"]."/videos/".$uploadVideo; $imagen_directorio = "include/database/usuarios/".$_SESSION["usuario"]."/videos/".$uploadImagen; DataBase::SubirVideo($titulo, $video_directorio, $imagen_directorio, $_SESSION["usuario"], $descripcion); $error = "<div id='error_alert'><div class='error_container'><i class='fa fa-times awesome error_close btn'></i><div class='error_text'>Tu video ha sido subido</div></div></div>"; } else { $error = "<div id='error_alert'><div class='error_container'><i class='fa fa-times awesome error_close btn'></i><div class='error_text'>El video debe estar en formato .mp4</div></div></div>"; } } } else { $error = "<div id='error_alert'><div class='error_container'><i class='fa fa-times awesome error_close btn'></i><div class='error_text'>La imagen pesa demasiado</div></div></div>"; } } else { $error = "<div id='error_alert'><div class='error_container'><i class='fa fa-times awesome error_close btn'></i><div class='error_text'>La imagen debe ser .jpg </div></div></div>"; } } return $error; } 

HTML

 <div class="upload_video_fields"> <div class="container"> <form action="upload.php" method="POST" enctype="multipart/form-data"> <div class="video_form left"> <input type="text" id="video_title" name="video_title" placeholder="Título del video..." required /> <textarea id="video_description" name="video_description" placeholder="Descripción del video..." cols="40" rows="6" aria-required="true" required></textarea> </div> <div class="video_form right"> <input type="file" id="video_imagen" name="video_imagen" required /> <input type="file" id="video_file" name="video_file" required /> </div> <input type="submit" id="boton_upload_video" name="boton_upload_video" class="btn" value="Subir" /> </form> </div> <div class="upload_progress"><div class="bar"></div></div> </div> 
18
  • no, that is not possible. The file is firstly accessed through the $_FILES global and secondly it would be available after upload. Once the file is uploaded it is straightforward to return the sizes but not before afaik! Commented Aug 29, 2015 at 16:09
  • So, how can I check if the image's dimensions are 320x180? Commented Aug 29, 2015 at 16:10
  • Exactly where are you thinking you want to put the size check? Commented Aug 29, 2015 at 16:11
  • 1
    After submission/upload you can check and if you want you could resize it to those dimensions Commented Aug 29, 2015 at 16:12
  • Hang on. Are you trying to use the $_FILES array in a script that was not used to do the actual UPLOAD? database.php looks like an odd script filename to be doing the actual uploading process???? Commented Aug 29, 2015 at 16:17

2 Answers 2

1

For multiple file uploads the following is from the manual - just in case you haven't found it. I hope that this provides sufficient guidance when trying to access the variables after upload.

----------------------------------- Example #1 Uploading multiple files ----------------------------------- <form action="file-upload.php" method="post" enctype="multipart/form-data"> Send these files:<br /> <input name="userfile[]" type="file" /><br /> <input name="userfile[]" type="file" /><br /> <input type="submit" value="Send files" /> </form> 

When the above form is submitted, the arrays $_FILES['userfile'], $_FILES['userfile']['name'], and $_FILES['userfile']['size'] will be initialized.Each of these will be a numerically indexed array of the appropriate values for the submitted files.

For instance, assume that the filenames /home/test/review.html and /home/test/xwp.out are submitted. In this case, $_FILES['userfile']['name'][0] would contain the value review.html, and $_FILES['userfile']['name'][1] would contain the value xwp.out.

Similarly, $_FILES['userfile']['size'][0] would contain review.html's file size, and so forth.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks too much, that what I wanted to know
0

The sql in the Subirvideo function looks incorrect as none of the values have quotes around them. Technically there is no need for the quotes around the integer values but can do no harm. I added in the exit statement to the sql so that you can check the result though you say it is fine now. The main method SubirVideoErrores has some additional code to resize the uploaded image to the specified max dimensions ( whilst hopefully mantaining the aspect ratio )

public static function SubirVideo( $titulo, $video_directorio, $imagen_directorio, $user, $descripcion ){ /* note: Ensure that $titulo and $descripcion are properly escaped to avoid issues with quote marks etc */ $sql = "INSERT INTO `video` ( `nombre`, `directorio`, `imagen`, `visitas`, `likes`, `usuario_id`, `fecha_subida`, `descripcion` ) VALUES ( '$titulo', '$video_directorio', '$imagen_directorio', '0', '0', '$user', now(), '$descripcion' )"; /* Remove if the exit( sql ) if it looks correct */ exit( $sql ); $resultado = self::Conexion( $sql ); return $resultado; } public static function SubirVideoErrores( $force_resize=true ){ /* set the default value of $force_resize to false if you do not wish resizing by default */ try{ $error = ""; $max_width=320; $max_height=180; /* I added this so I could set a var rather than use a session for testing, ie: $session_usuario='antonio' etc */ $session_usuario=$_SESSION["usuario"]; if( isset( $_POST["boton_upload_video"] ) ){ $titulo = $_POST["video_title"]; $descripcion = $_POST["video_description"]; $uploadImagen = $_FILES['video_imagen']['name']; $tipoImagen = $_FILES['video_imagen']['type']; $uploadVideo = $_FILES['video_file']['name']; $tipoVideo = $_FILES['video_file']['type']; /* Check for errors */ $video_upload_error = intval( $_FILES['video_file']['error'] ); $image_upload_error = intval( $_FILES['video_imagen']['error'] ); if( $video_upload_error > 0 or $image_upload_error > 0 ){ exit('There were errors during the upload process'); } if( !empty( $uploadVideo ) && !empty( $uploadImagen ) ){ $directorio = $_SERVER['DOCUMENT_ROOT']."/tapeHD/include/database/usuarios/".$session_usuario."/videos"; #$directorio = 'c:/temp/uploads_testing'; if( $tipoImagen !== "image/jpeg"){ $error = "<div id='error_alert'><div class='error_container'><i class='fa fa-times awesome error_close btn'></i><div class='error_text'>La imagen debe ser de formato .jpg</div></div></div>"; print $error; } else { list( $width, $height, $type, $attr ) = getimagesize( $_FILES["video_imagen"]["tmp_name"] ); if( $force_resize==true ){ /* Resize supplied mage to a maximum of 320x180 if it is larger then these sizes */ if( $width > $max_width or $height > $max_height ) { if( $width > $height ){ /* landscape */ $aspect = $max_width / $width; $new_width = $max_width; $new_height = $height * $aspect; } elseif( $width == $height ){ /* square */ $new_width=$max_height; $new_height=$max_height; } else { /* portrait */ $aspect = $max_height / $height; $new_width = $width * $aspect; $new_height = $max_height; } $uploadedimgpath=$directorio."/".$uploadImagen; move_uploaded_file( $_FILES['video_file']['tmp_name'], $directorio."/".$uploadVideo ); move_uploaded_file( $_FILES['video_imagen']['tmp_name'], $uploadedimgpath ); $image = @imagecreatetruecolor( $new_width, $new_height ); $background_color = @imagecolorallocate( $image, 255, 255, 255 ); $source = @imagecreatefromjpeg( $uploadedimgpath ); @imagecopyresampled( $image, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); @imagejpeg( $image, $uploadedimgpath, 100 ); @imagedestroy( $image ); @imagedestroy( $source ); $width=$new_width; $height=$new_height; } } else { $uploadedimgpath=$directorio."/".$uploadImagen; move_uploaded_file( $_FILES['video_file']['tmp_name'], $directorio."/".$uploadVideo ); move_uploaded_file( $_FILES['video_imagen']['tmp_name'], $uploadedimgpath ); } if( $_FILES['video_imagen']['size'] > 1024000 ){ $error = "<div id='error_alert'><div class='error_container'><i class='fa fa-times awesome error_close btn'></i><div class='error_text'>La imagen pesa demasiado</div></div></div>"; print $error; } else { if( $width == 320 or $height == 180 ){ if( $tipoVideo == "video/mp4" ){ $video_directorio = "include/database/usuarios/".$session_usuario."/videos/".$uploadVideo; $imagen_directorio = "include/database/usuarios/".$session_usuario."/videos/".$uploadImagen; DataBase::SubirVideo( $titulo, $video_directorio, $imagen_directorio, $session_usuario, $descripcion ); $error = "<div id='error_alert'><div class='error_container'><i class='fa fa-times awesome error_close btn'></i><div class='error_text'>Tu video ha sido subido</div></div></div>"; print $error; } else { $error = "<div id='error_alert'><div class='error_container'><i class='fa fa-times awesome error_close btn'></i><div class='error_text'>El video debe estar en formato .mp4</div></div></div>"; print $error; } } else { $error = "<div id='error_alert'><div class='error_container'><i class='fa fa-times awesome error_close btn'></i><div class='error_text'>La imagen debe de ser 320x180</div></div></div>"; print $error; } } } } } }catch( Exception $e ){ exit( $e->getMessage() ); } } 

I have tested this and the image and video both get uploaded fine on my system ( with some path changes of course ) and both are readable. I can watch the uploaed video and view the uploaded image - even those that get resized. Is the video you upload very large?

9 Comments

OK, it worked perfectly @RamRaider. It inserts me the different values in my database. The problem is that the files I move, get damage. I dont know why. I can't watch the video, I can't even see the video's picture. What could it be?
Its size is 54MB @RamRaider. I have Windows 10 by the way, I dont know if that could be the problem
You have made sure that upload_max_filesize is set to a large enough value and that max_execution_time is also set large enough? Certainly 54Mb does not seem excessive but you must ensure the post can handle file more than the default 2Mb
max_execution_time = 120 AND upload_max_filesize = 100M @RamRaider
or use the php command: ini_set('upload_max_filesize',pow( 1024, 100 ) ); etc ~ that would, I think, set a 100Mb limit. Similarly for the time limit, ini_set('max_execution_time',180); and ini_set('max_input_time',180); etc
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.