Page 1 of 2

PHP3 but not PHP4

Posted: Tue Nov 18, 2003 3:12 pm
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>


?>

Posted: Tue Nov 18, 2003 3:27 pm
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'])) {

Posted: Tue Nov 18, 2003 3:54 pm
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?

Posted: Tue Nov 18, 2003 4:06 pm
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

Posted: Tue Nov 18, 2003 4:13 pm
by jbatty
Yes the folder has chmod777 on it.

Posted: Tue Nov 18, 2003 4:36 pm
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.

Posted: Tue Nov 18, 2003 5:33 pm
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/.

Posted: Wed Nov 19, 2003 2:02 am
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]

Posted: Wed Nov 19, 2003 3:00 am
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

Posted: Wed Nov 19, 2003 3:07 am
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

Posted: Wed Nov 19, 2003 3:36 am
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

Posted: Wed Nov 19, 2003 5:51 am
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>

?>

Posted: Wed Nov 19, 2003 6:01 am
by twigletmac
Have you tried using [php_man]move_uploaded_file[/php_man]() instead of copy()?

Mac

Posted: Wed Nov 19, 2003 6:09 am
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

Posted: Wed Nov 19, 2003 6:31 am
by twigletmac
Is safe mode on on both servers?

Mac