0

I'm trying to make an uploader for PDF file's and I don't know why when I work in localhost everything's ok, but when I work with my paid hosting the function move_uploaded_file don't work.

Here's my code:

<?php
$empresa = $_POST['name'];
$direccion = $_POST['direccion'];
$codigopostal = $_POST['codepostal'];
$poblacion= $_POST['poblacion'];
$email = $_POST['email'];
$telefono = $_POST['telefonol'];
$trabaja = $_POST['jobornot'];
$horarios = $_POST['horarios'];
$enque = $_POST['seleccion'];
$permisoconducir = $_POST['conducir'];
$uploaddir = '../tmp_cv/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

$sujeto = "Curriculum Vitae Baby & Home";
$from = "recursoshumanos@babyandhome.es";
$headers = "From:" . $from;
$cuerpo = "Empresa: ".$empresa."\n\nDirección:".$direccion."\n\nPoblación:\n".$poblacion."\n\nCodigo Postal:\n".$codigopostal."\n\nemail:\n".$email."\n\nTeléfono:\n".$telefono."\n\nTrabaja?:\n".$trabaja."\n\nEn que?:\n".$enque."\n\nPermiso de conducir?:\n".$permisoconducir."\n\nHorarios disponibles:\n".$horarios."";
$to = 'recursoshumanos@babyandhome.es';

$cuerpo_automatico = "";


if($empresa==NULL or $direccion==NULL or $poblacion==NULL or $codigopostal==NULL or $email==NULL or $telefono==NULL) {
    echo 'Todos los campos con un (*) son obligatorios';
}
elseif($_FILES['userfile']['size'] > 5000000) {
        echo "El tamaño del pdf es demasiado grande y debe ser como máximo de 5Mb";
}
        elseif(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        require 'PHPMailerAutoload.php';
        require_once('class.phpmailer.php');
        $mail = new PHPMailer;
        $mail->setFrom('recursoshumanos@babyandhome.es', 'Recursos Humanos Baby & Home');
        $mail->addAddress($email, '');
        $mail->Subject = 'Curriculum Vitae Baby & Home';
        $mail->msgHTML("Muchas gracias por hacernos llegar su CV a Baby & Home. Revisaremos su candidatura y en el momento que exista una oferta adecuada a su perfil nos pondremos en contacto con usted.");
        $mail->addAttachment($uploadfile, '');

        if (!$mail->send()) {
            $msg = "Error: " . $mail->ErrorInfo;
        } else {
            echo'Mensaje enviado correctamente';
        }
    }
    else {
    echo'Ha habido un error, porfavor vuelve a enviar tu mensaje';
}

?>

I think that fails here:

elseif(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

because don't enter there and shows echo.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
seergiue
  • 326
  • 1
  • 3
  • 15
  • 4
    Are the permissions on your host the same as they are on your local machine? There should be some helpful information in the error log. – Jay Blanchard Jul 02 '15 at 12:55
  • Is it shared hosting? Probably lacking the rights to create a file at the target directory. – hiergiltdiestfu Jul 02 '15 at 12:55
  • Where can I check the permission? php info file? I check there and everything's the same. I edited php.ini too. – seergiue Jul 02 '15 at 12:55
  • *Where can I check the permission?* ... Navigate to the `tmp_cv/` directory you've specified (through (S)FTP probably) and see what the *nix owner/group permissions look like; you'll probably see something like `www-data www-data drwxr-x---`; the first part is the file owner the second the group and the final bit the actual permissions on that directory... I'm guessing you'll probably want `drwxrwx---` (770) - though there are security implications in having a file that the Apache/PHP user can write to being under the document root (e.g. it can be read over http). – CD001 Jul 02 '15 at 13:09
  • @CD001 In filezilla says the permissions are flcdmpe(0755). Is this okay? – seergiue Jul 02 '15 at 13:14
  • Depends on *who* owns the directory; if the Apache/PHP user owns it then it should be. If it's owned by another user in the same group you can chmod it to 775 (though really the *nobody* user shouldn't actually need any permissions) - there should never be any reason to 777 anything but you can try it as a quick test to see if it's a permissions issue... just don't leave it like that ;) – CD001 Jul 02 '15 at 13:20
  • @CD001 I changed the permissions but the error persist. Maybe the error is in the ajax? – seergiue Jul 02 '15 at 13:23
  • Well that pretty much rules out a *nix permissions issue then ;) – CD001 Jul 02 '15 at 13:25
  • Quite possibly - last time I wrote a "dynamic" file uploader I had to use an iframe - it wasn't supported in Ajax it was that long ago; take a look at : http://stackoverflow.com/questions/2320069/jquery-ajax-file-upload – CD001 Jul 02 '15 at 13:28

1 Answers1

0

I searched and I think my problem isn't in PHP, is in Ajax/jQuery.

I use this code:

function enviarCV () {
 $("#send").html("Enviando..");
 $.ajax({
   url: "enviarCorreoCV.php",
   type: 'POST',
   data: $('#form2').serialize(),
   cache: false,
   success: function(html){
    $("#send").html("Enviar");
    alert(html);
     }
  }); 
}

Maybe I can't use serialize() with upload files?

seergiue
  • 326
  • 1
  • 3
  • 15