Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to upload files to a database using a class method
    text
    copied!<p>I'm trying to upload image files to a database using a method inside a class. It should work like a function.</p> <p>I have 2 php files. The first one, the file that receives the image and calls the method:</p> <pre><code>&lt;?php if (!empty($_FILES)){ require_once('masterclass.php'); $galeria = $_POST['gal']; $fecha = $_POST['fecha']; $titulo = $_FILES['Filedata']['name']; $imagen = $_FILES['Filedata']['tmp_name']; $mc = new masterclass; $mc-&gt;saveImage($galeria, $fecha, $titulo, $imagen, 800, 600, 80); } ?&gt; </code></pre> <p>The second one, the method that should do the job:</p> <pre><code>public function saveImage($galeria, $fecha, $titulo, $tmpImagen, $maxWidth, $maxHeight, $quality){ if( isset($galeria, $fecha, $titulo, $tmpImagen, $maxWidth, $maxHeight, $quality) ){ $ms-&gt;dbc(true); // connect to database $fecha = $this-&gt;formatFecha($fecha); // format date string $origen = imagecreatefromjpeg($tmpImagen); $x = imagesx($origen); $y = imagesy($origen); if( $x &gt; $maxWidth ){ $nx = $maxWidth; $ny = $y*$nx/$x; } if( $y &gt; $maxHeight ){ $ny = $maxHeight; $nx = $x*$ny/$y; } else{ $nx = $x; $ny = $y; } $destino = imagecreatetruecolor($nx,$ny); imagecopyresized($destino,$origen,0,0,0,0,$nx,$ny,$x,$y); imagejpeg($destino, $tmpImagen, $quality); $imagen = addslashes(fread(fopen($tmpImagen, "rb"), filesize($tmpImagen))); $peticion = "insert into galeria_fotos (galeria, fecha, titulo, imagen) values ('$galeria', '$fecha', '$titulo', '$imagen')"; mysql_query($peticion) or die($this-&gt;isError(2)); $ms-&gt;dbc(false); // disconnect from database } else{ die($this-&gt;isError(2)); } } </code></pre> <p>If I do the method's job directly in the first php file it works. But it doesn't when I use saveImage() from the class. I'm working since the last year only with JavaScript and I'm cold with PHP.</p> <p>Sorry for the variable names in spanish, but I think that is totally understandable.</p> <p>As additional information I can say that I tried sending directly the <code>$_FILES</code> array when calling the method, with no results.</p> <p>Hope you can help me. Thank you all. Regards :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload