Sep 9, 2011

Fast PHP example - FTP file upload

Here is one fast and simple example on how to do FTP file uploads with PHP.
<?php

$ftp_server = "ftp.server.url";

$ftp_user = "user";
$ftp_password = "password";

$file_to_upload = "localfile.txt";
$remote_location = "/location/file.txt";

// set up connection or exit with message
$flink = ftp_connect($ftp_server) or exit("Can't connect to ftp server: $ftp_server");

// login or at least try  
if(ftp_login($flink, $ftp_user, $ftp_password)) {

 
 // if login successful use ftp_put to upload the file
 // if you upload binary files use mode FTP_BINARY
 if(ftp_put($flink, $remote_location, $file_to_upload, FTP_ASCII)) {

  echo "Success! File is uploaded!";
 } else {
  echo "Can't upload file";

 }
} else {
 echo "Can't login with this user & password";

}

// close the connection
ftp_close($flink);

?>
You can get more information at http://www.php.net/manual/en/ref.ftp.php

Aug 19, 2011

PHP : Handling file uploads - easy tutorial with example

Handling file uploads with PHP is very easy task and here you will learn how to do it. And because I believe that people learn best with examples lets start with this.
Now lets upload one file to some user directory on a server. Lets create file upload_form.php on our server. It will contain mainly html code:

upload_form.php
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">

    File: <input name="ourfile" type="file" />
    <button type="submit">Upload</button>

</form>
</body>
</html>

Its very simple file with only one form. Don’t forget to add enctype="multipart/form-data" to your form when its used for file uploads. In this form we have to fields - the first is our file and the second is the submit button.
Now we need another file upload.php. Here is its content

upload.php
<?php
$dir = '/var/www/files/';

$destinationfile = $dir . basename($_FILES['ourfile']['name']);

if (move_uploaded_file($_FILES['ourfile']['tmp_name'], $destinationfile)) {

   echo "Upload successful!";
} else {
   echo "File attack!\n";

}
?>

Here we have destination directory for our uploads - $dir = '/var/www/files/'. Our file will be saved in this directory and will have the name of the uploaded file.
The function move_uploaded_file checks if the file (first parameter) is a valid uploaded file and if its valid it moves it to $destinationfile. If you just want to check the file without moving it use is_uploaded_file instead.
And that’s all. Now if you have done everything the right way you must see “Upload successful!” when you use upload_file.php and upload file.

The original of this tutorial is here http://ldeveloper.blogspot.com/2011/08/php-and-handling-file-uploads-easy.html

Aug 16, 2011

PHP: Playing with StdClass

What is StdClass and where does it live?

StdClass is a document class in PHP with no predefined members. Its the class that is created when you cast something to object like this:

$obj = (object) $something;

Sounds good but how to get its members if we don’t know about them? Actually, its very easy and we have several approaches.

First, we can iterate through its members with foreach:
foreach ($obj as $key => $value) {
   
}
Second we can cast our object to array:

$array = (array) $obj;

Or get its members as array:

get_object_vars($obj);

What else we can do with StdClass?
We can create objects and assign members:
$std = new StdClass();
$std->key = "value";
and access them:
echo $std->key;
If you see an error like this:
Catchable fatal error: Object of class stdClass could not be converted to string
the culprit is our stdClass of course. Maybe you are trying to print an object of this class.

Read how to use SOAP with PHP

Popular Posts