PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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:
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]
...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/"?
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.
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.
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.
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
//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
<?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");
?>