Page 1 of 1

Problem uploading a File

Posted: Wed Jul 21, 2010 6:10 am
by Ichi
Hello.

First off all, i'll try to explain myself quite as possible since i'm really new into PHP programming and only know really small/basic things.

Well, the problem is that i'm trying to do a php file that can upload a file (a photo) into a server and at the same time grab the name of the photo and insert into a mysql database. Anyway, i'm only giving you the php code, since i'm not having any trouble with the mysql part.
<?php
$dir = "SGE/colaborador/images/";
$dir = $dir . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file( $_FILES['uploadedfile']['tmp_name'], $dir)) {
echo "O ficheiro ". basename( $_FILES['uploadedfile']['name']). " foi descarregado e a informação adicionada à Base de Dados"; }
else{
echo "Existe um problema no upload da foto.";
}
?>
ERROR: Warning: move_uploaded_file(SGE/colaborador/imagesfoto.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/sge/public_html/colaborador/upload.php on line 8

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phptJTqOf' to 'SGE/colaborador/imagesfoto.jpg' in /home/sge/public_html/colaborador/upload.php on line 8
Existe um problema no upload da foto.


I thing it has something to do with permissions, but i'm not quite sure. Really don't know what to do. It would be great an answer.

Hope i gave enough information.

P.S1.: In this part i'm only testing the code in local directory, not in the actual server. But just so you to know... i have the same error when i change directory.

P.S2.: I think it's not necessary the html, as i assure is nothing to do with that, lol.

Re: Problem uploading a File

Posted: Wed Jul 21, 2010 12:04 pm
by Jade
Check the path to the directory, also check the permissions on the folder and make sure it's chmod 777.

Re: Problem uploading a File

Posted: Wed Jul 21, 2010 3:30 pm
by Ichi
I have done that. The problem was that i had no permissions and i was not writing the correct path.

Anyway, thanks for having the trouble of responding. :)

Re: Problem uploading a File

Posted: Fri Jul 23, 2010 4:44 am
by Ichi
Hi there. People, i have another doubt concerning the upload photo once again. I'm trying to implement part of the code, that i showed you before, in a project that already exists but the result is not being the same. I'm going to show you what i think it is the crucial part. Mysql part is working, it's only the upload part.

And don't ask me to do it separately, only the upload part in a different php file because the point is to use it in a input (type = file, the photo part) already inside of a form. I can't use the action of that form, cuz i have others inputs.

Hope this was helpful information. Here is the code:
$dir = '../img/colaboradores/';
$dir = $dir . basename( $_FILES['foto']['name']);
$foto=($_FILES['foto']['name']);

if(empty($_SESSION['erro'])) {
$sql = sprintf("INSERT INTO colaborador (username, passwd, nome, data_nasc, telefone, telemovel, email, data_inicio, estado, cargo, id_direccao, id_area, foto)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
$_POST['username'], $password, $_POST['nome'], $_POST['data_nascimento'], $_POST['telefone'], $_POST['telemovel'], $_POST['email'], date('Y-m-d'), 'activo', $_POST['cargo'], $_POST['direccao'], $_POST['area'], $_POST['foto']);

$db->insert($sql);

$last_id = $db->fetch_reg("SELECT LAST_INSERT_ID() AS id");
$last_id = $last_id[0]['id'];

$date = new DateTime();
$date->modify('+2 month');
$sql = sprintf("INSERT INTO passwords VALUES ('', '%s', '%s', '%s', '%s')", $_GET['id'], $password, date('Y-m-d H:i:s'), $date->format('Y-m-d H:i:s'));
$db->insert($sql);

if($_POST['cargo']>1) {
$sql = "INSERT INTO perfil (colaborador, cl_criar, cl_editar, cl_ver, proj_criar, proj_editar, proj_ver, tarefa_criar, tarefa_editar, tarefa_ver, relatorios, facturacao, col_criar, col_editar, folha_horas, alertas, pep, perm) VALUES (".$last_id.",'0','0','1','0','0','1','1','0','0','0','0','0','0','0','0','0','0')";
$db->insert($sql);
}
else {
$sql = "INSERT INTO perfil (colaborador, cl_criar, cl_editar, cl_ver, proj_criar, proj_editar, proj_ver, tarefa_criar, tarefa_editar, tarefa_ver, relatorios, facturacao, col_criar, col_editar, folha_horas, alertas, pep, perm) VALUES (".$last_id.",'1','1','2','1','1','2','1','1','1','1','1','1','1','1','1','1','1')";
$db->insert($sql);
}

}

if(move_uploaded_file($_FILES['foto']['tmp_name'], $dir)){
echo "O ficheiro". basename($_FILES['foto']['name']). "foi descarregado com sucesso";
}
else {
echo "Desculpe, a foto não foi descarregada";
}
//echo '<script language="javascript" type="text/javascript">window.location.href="index.php";</script>';
}
P.S.: Sorry the double post.