PHP3 but not PHP4

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!

Moderator: General Moderators

jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

PHP3 but not PHP4

Post by jbatty »

This script works well under PHP3 but not under PHP4.2.3. Can anyone spot anything wrong or is it due to the configuration of my PHP4.

Thanks

Code: Select all

<?php
<html>
<head><title>UPL TEST</title> </head>
<body>
<?php

if ( !isset( $submit)) {
  echo "
  <h3>Try upload</h3>
  <form method='post' enctype='multipart/form-data' action='$PHP_SELF'>
    <input type='file' name='userfile'>
    <input type='submit' name='submit'>
  </form>
";

} else {

  echo "<p>original name = $userfile_name";
  echo "<p>uploaded to php system pathname: $userfile";

  // absolute path 
$upfile = "/path/to/my/upload/directory/uploads/".$userfile_name;

  // copy uploaded file from php system upload directory to your own
  // and give it its original extension back.
  if ( !copy( $userfile, $upfile)) {
    echo "Failed to copy file $userfile to $upfile.$userfile_name";
  } else {
    echo "<p>moved to $userfile_name";
  }

}

?>

</body>
</html>


?>
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

you can no longer access post or get vars with just the var name
you have to use either

$_GET['field'] OR $_POST['field']

so instead of looking for $submit you need to look for
if ( !isset( $_POST['submit'])) {
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

Post by jbatty »

Thank you, jplush76

The script responded this time. However, it would not move the uploaded file from temp dir into the directory specified.

I am getting the following message.

Code: Select all

Warning: Unable to access in /users/.public_html/cgi-bin/uploadtest.php4 on line 28
Failed to copy file to "/path/to/my/upload/directory/uploads/.
Even though it worked OK under PHP3!
What setting do i need to change in my php.ini file?
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

do you have permission set on the new folder?
you'll have to use CHMOD and set it to be able to write to the folder "uploads".

let me know if that helps
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

Post by jbatty »

Yes the folder has chmod777 on it.
Quietus
Forum Newbie
Posts: 16
Joined: Sat Nov 15, 2003 1:59 am

Post by Quietus »

This looks like a variable typo to me:

I'm assuming you used $_POST["variable"] as was suggested before:

Code: Select all

$_POST["upfile"] = "/path/to/my/upload/directory/uploads/".$_POST["userfile_name"];
In your form, you submit the variable "userfile" yet you're calling "userfile_name"

Please forgive me if I'm barking up the wrong tree.
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

Post by jbatty »

Thanks Quietus


Here is the PHP script as I have rewritten it for PHP4

Code: Select all

<?php
<html>
<head><title>UPL TEST</title> </head>
<body>
<?php


if ( !isset( $_POST['submit'])) {

  echo "
  <h3>Try upload</h3>
  <form method='post' enctype='multipart/form-data' action='$PHP_SELF'>
    <input type='file' name='userfile'>
    <input type='submit' name='submit'>
  </form>
";

} else {

  echo "<p>original name = $_POST[$userfile_name]";
  echo "<p>uploaded to php system pathname: $_POST[$userfile]";

  // absolute path 
$upfile = "/path/to/my/upload/directory/uploads/".$_POST[$userfile_name];

  if ( !copy( $_POST[$userfile], $upfile)) {
    echo "Failed to copy file $_POST[$userfile] to $upfile.$_POST[$userfile_name]";
  } else {
    echo "<p>moved to $_POST[$userfile_name]";
  }

}

?>

</body>
</html>

?>
But i still got the error message

Code: Select all

original name =
uploaded to php system pathname:
Warning: Unable to access in /users/.public_html/cgi-bin/uploadtest.php4 on line 28
Failed to copy file to /path/to/my/upload/directory/uploads/.
Quietus
Forum Newbie
Posts: 16
Joined: Sat Nov 15, 2003 1:59 am

Post by Quietus »

I'm afraid I'm still not seeing where userfile_name is coming from. But maybe that's because I only just woke up.

Also your calls to $_POST need to be in the form $_POST["variable"] not $_POST[$variable]
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Hi, you need to have a read of this:
http://php.net/manual/en/features.file-upload.php
as it explains file uploads using PHP 4 and how to use the $_FILES array.

Mac
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

/users/.public_html/cgi-bin/uploadtest.php4
Can you try copying it to something else like /.public_html/uploads rather than the CGI-BIN? It might be the error.

-Nay
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

there are a few things that i can see:

You are using post wrong, should be like this

Code: Select all

$_POST['userfile']
but, you should even do it that way, should be like this:

Code: Select all

$_FILES['userfile']['tmp_name']
Read the link that Mac gave you and all will be clear! :)

Mark
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

Post by jbatty »

Thanks to all the contributors to this thread so far.

I have rewritten the script according to suggestions. But it still says it could not access the folder that it was able to access on the PHP3 platform.

Code: Select all

<?php
<html>
<head><title>UPL TEST</title> </head>
<body>
<?php


if ( !isset( $_POST['submit'])) {

  echo "
  <h3>Try upload</h3>
  <form method='post' enctype='multipart/form-data' action='$PHP_SELF'>
    <input type='file' name='userfile'>
    <input type='submit' name='submit'>
  </form>
";

} else {

  echo "<p>original name = $_FILES['userfile']['name']";
  echo "<p>uploaded to php system pathname: $_FILES['userfile']['tmp_name']";

 
$upfile = "/path/to/my/upload/directory/uploads/".$_FILES['userfile']['name'];
 
  if ( !copy( $_FILES['userfile']['tmp_name'], $upfile)) {
    echo "Failed to copy file $_FILES['userfile']['tmp_name'] to $upfile.$_FILES['userfile']['name']";
  } else {
    echo "<p>moved to $_FILES['userfile']['name']";
  }

}

?>

</body>
</html>

?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you tried using [php_man]move_uploaded_file[/php_man]() instead of copy()?

Mac
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

Post by jbatty »

I have just found that the script works on another host's server running PHP4.2.3. But still would not work on my University's PHP 4.2.3. If there any setting(s) that I can get the webmaster to turn on / off?
I have looked at the phpinfo() for the server, and file_uploads is set to 1. The rest of the PHPinfo can be found here.

http://www.lsbu.ac.uk/php4-cgiwrap/spider/phpinfo.php4
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Is safe mode on on both servers?

Mac
Post Reply