Page 1 of 1
File Uploads
Posted: Thu Sep 21, 2006 10:41 am
by mikeeeeeeey
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi there.
I'm having a wee bit of a problem with file uploads. Previously in the past I've been using the function 'copy' but I've now been advised to move to 'move_uploaded_file'.
However, both of these methods work on my testing server, yet none will upload the file when put onto the remote server. I'm getting an error code of 0 in the $_FILES array, so that must be fine, and I'm thinking its something to do with the path.
Here's my code:
Code: Select all
$uploadDir = "uploads/";
$uploadFile = $uploadDir . basename($_FILES['file_to_upload']['name']);
if (move_uploaded_file($_FILES['file_to_upload']['tmp_name'], $uploadFile))
{
print("success");
}
else
{
print("failure");
}
The folder 'uploads' in is the root of my site.
Anyone have any ideas?
Thanks for looking

)
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Thu Sep 21, 2006 10:58 am
by opengavel
My non-pro troubleshoot would be:
...to replace the script with one that will echo the dir you are in to confirm the correct path. I have gotten lost pathwise a few times and I found that echo-ing the dir is a simple way of making sure you are pointed at the right place, assuming there are files or sub-dir there that will confirm where you are.
...Not sure of this but does the absence of the intial "/" make a difference? Should it be "/uploads" instead of "uploads/"?
Posted: Thu Sep 21, 2006 11:05 am
by mikeeeeeeey
I've already tried outputting the current directory, using $_SERVER['DOCUMENT_ROOT'] and it's not worked.
But good call though, I really should have put it in the original post.
Posted: Thu Sep 21, 2006 2:47 pm
by Mordred
Set the proper write permissions of the uploads/ dir. Search the forum for details, there are security issues with choosing the correct permissions. Check the chown() and chmod() functions.
Another reason might be that the folder is created by FTP, which is a different user than PHP/Apache - check chown() for details.
Posted: Fri Sep 22, 2006 4:18 am
by mikeeeeeeey
Still not worked, I've tried both before.
I've even tried using chdir() and then getcwd() to output the full document root. It can't be anything to do with write permissions since it was working fine at one point, and then suddenly broke.
Unless someone/thing changed the write permissions without telling me...hmmm..
Anyways, I'm here all day anyone has any more ideas.
Posted: Fri Sep 22, 2006 4:21 am
by n00b Saibot
please provide any error messages that you get from the code...
Posted: Fri Sep 22, 2006 4:26 am
by ibbo
You can try echo system("pwd"); and see if that works.
Ive ran into some ISP where it been a nightmare to chown chgrp etc. Even directories ive set up locally have been reset when uploaded leaving me frustrated.
At the end of the day you can build in an ftp fail safe for when move_upload_file fails.
Code: Select all
class FTP{
private $_host="127.0.0.1";
private $_user="";
private $_pass="";
public $conn;
public $login_result;
public function __construct(){
$this->conn = ftp_connect($this->_host);
$this->login_result = ftp_login($this->conn, $this->_user, $this->_pass);
ftp_pasv($this->conn, true);
}
public function chdir($path){
ftp_chdir($this->conn,$path);
}
public function save($from, $to){
if(ftp_put ($this->conn, $to, $from, FTP_BINARY))
return true;
else
return false;
}
public function close(){
ftp_close($this->conn);
}
}
if (move_uploaded_file($this->ImgFrom, $this->path.$this->ImgTo)) {
return true;
}else{
// Host dont let us chgrp so we use ftp as a work around
try{
$ftp = new FTP();
$ftp->chdir($this->path);
if(!$ftp->save($this->ImgFrom, $this->ImgTo)){
$ftp->close();
return false;
}else{
$ftp->close();
return true;
}
}catch(exception $e){
echo $e;
}
}
$this->path is the location you are saving to
$this->ImgFrom is your $_FILES['uploadimg']
$this->ImgTo is the filename you want to save it as
Ibbo
Posted: Fri Sep 22, 2006 4:30 am
by n00b Saibot
note: the above class/script will run only in PHP5+
Posted: Fri Sep 22, 2006 7:00 am
by mikeeeeeeey
There isn't a system error message, I'm just getting the wrong half of the argument back i.e.
Code: Select all
//upload my file pleaaaaaaaaaaaase
if (!move_uploaded_files($_FILES['file_to_upload']['tmp'], "../assets/" . $_FILES['file_to_upload']['name']))
{
echo "This sucks because it doesn't work";
}
Even if I try print_r($_FILES['file_to_upload']); I get ['error'] = 0, which means theres nothing wrong with the file or the array, it's just the path.
Thanks for the class by the way, but surely there's just something small I'm missing

Posted: Fri Sep 22, 2006 8:48 am
by aquanutz
This is something I wrote a few months back that may be able to help you:
Code: Select all
<?php
$target = "pictures/";
$target = $target . basename( $_FILES['uploaded']['name']);
$name = basename( $_FILES['uploaded']['name']);
$ok=1;
//This is our size condition
if ($uploaded_size > 500000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ".basename( $_FILES['uploadedfile']['name']). " has been uploaded";
echo $name;
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
session_start();
$link = mysql_connect("localhost", "user", "pass");
if(! $link)
die("Couldn't connect to DB: " .mysql_error());
$db = "pictures";
mysql_select_db($db) or die("Couldn't open DB: " .mysql_error());
$query = "INSERT INTO pictures (file) VALUES ('$name')";
$result = mysql_query($query) or die("Invalid query: " . mysql_error());
header("location: pictures.php");
?>