Copying files into a newly created folder

I'm having a problem creating folders and then moving images that are uploaded from a page into that folder.  I keep getting this error: Warning: copy(./$MemberName/images/image.jpg): failed to open stream: No such file or directory in /home/bigboost/public_html/members/publishform2.php on line 101.  But I clearly open and create the folders in the first four lines of code.  I labeled line 101, the problem line.  Can anyone see what I'm doing wrong and how to fix it?  The folders are created just fine, it's just that the image won't go into them.

Thanks for looking,
Erica

Here is my code:
<?php
mkdir("./$MemberName/", 0777);
chmod("./$MemberName/", 0777 );
mkdir("./$MemberName/images/", 0777);
chmod("./$MemberName/images/", 0777 );
?>
<?php
if (file_exists("./$MemberName/index.php")){
print("A gallery for $MemberName already exists.<br>");
}
else {
$path_to_file = './$MemberName/images/';
$files = $_FILES['files'];

if (!ereg("/$", $path_to_file))
        $path_to_file = $path_to_file."/";
$i = 0;
$imgname = array();
foreach ($files['name'] as $key=>$name) {
  if ($files['size'][$key]) {
     // clean up file name
    $name = ereg_replace("[^a-z0-9._]", "",
      str_replace(" ", "_",
          str_replace("%20", "_", strtolower($name)
          )
       )
     );
     $location = $path_to_file.$name;
//         while (file_exists($location))
//        $location .= ".copy";
     copy($files['tmp_name'][$key],$location);  <----Line 101, problem line
     $imagesToDisplay[] = $location;
   unlink($files['tmp_name'][$key]);
     echo "\n
 Successfully uploaded file: $name.<br>";      
      $imgname[$i++] = $name;

  }
}
}
?>
Job4SuprmnAsked:
Who is Participating?
 
Marcus BointonConnect With a Mentor Commented:
Your foreach looks funny to me - to handle multiple uploads, I would expect you to be looping through $_FILES itself. there's a whole of stuff that leads on from that. I don't think I would expect $_FILES['files'] to be defined at all; see http://www.php.net/manual/en/features.file-upload.php

Or is it that you have assigned multiple upload fields to an HTML array? If so, I'm not sure why you'd bother when $_FILES does it for you.

Judging by the error message, that variable name should never appear in there, only its value, so I suspected your quoting, and sure enough:

$path_to_file = './$MemberName/images/';

should be:

$path_to_file = "./$MemberName/images/";
0
 
Job4SuprmnAuthor Commented:
That worked, but now when I try to call the images back up it doesn't work...I just get nothing.  Am I calling them right?

if (is_file("/members/$MemberName/images/${imgname[0]}")) {
print("<a href=\"/members/$MemberName/images/${imgname[0]}\" target=\"_blank\"><img border=\"1\" src=\"/members/$MemberName/images/${imgname[0]}\" width=\"70\" height=\"52\"></a>");
}
else {
print("<img src=\"/members/images/blank.gif\">");
}
0
 
Marcus BointonCommented:
No, you've got the quoting syntax wrong:

${imgname[0]}

should be

{$imgname[0]}
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Job4SuprmnAuthor Commented:
I've narrowed it down to this statement:
if (is_file("$MemberName/images/{$imgname[0]}")) {

I'm assuming it's something to do with $MemberName.  The directory should be right
0
 
techtonikCommented:
Where did you get this $MemberName ?
It seems, that you 've got this from your $_GEt or $_POST arrays. In that case add
    $MemberName = $_GET['MemberName'];
at the beginning of your file.
0
 
Job4SuprmnAuthor Commented:
$MemberName is submitted from a form.  The whole script is for a user submitting images and then them being posted on a newly created page named $MemberName.php.  $MemberName works throughout the whole script except for on the line:

if (is_file("$MemberName/images/{$imgname[0]}")) {

$MemberName is also used to create a folder where $MemberName.php is located.  Everything is created fine, I just can't get the part to work where if the image exists, output it.

if (is_file("$MemberName/images/{$imgname[0]}")) {
print("<a href=\"/members/$MemberName/images/{$imgname[0]}\" target=\"_blank\"><img border=\"1\" src=\"/members/$MemberName/images/{$imgname[0]}\" width=\"70\" height=\"52\"></a>");
}
else {
print("<img src=\"/members/images/blank.gif\" width=\"70\" height=\"52\">");
}
0
 
laurentyCommented:
try to do

print("$MemberName/images/{$imgname[0]}");

right before the is_file to see that the string looks like
0
 
aib_42Commented:
> That worked, but now when I try to call the images back up it doesn't work...

"call", hmm. Is your problem part (if is_file...) in a function other than the rest of the code, where $MemberName is defined? Trying to use variables from an outer scope without putting a "global $varname;" inside your function is one common, simple mistake.
0
 
Job4SuprmnAuthor Commented:
Ok, when I do the print("$MemberName/images/....); it prints out exactly like it's supposed to.  So I'm assuming that the if (is_file("MemberName/images/{$imgname[0]}")) is wrong because of the directory being wrong... What I mean is...

The form is at http://www.bigboosting.com/membergal.php
and is submitted to http://www.bigboosting.com/members/publishform2.php
A page is created at http://www.bigboosting.com/members/$MemberName/index.php
and images for that page go to http://www.bigboosting.com/members/$MemberName/images/

On http://www.bigboosting.com/members/$MemberName/index.php is where you'll find this portion of the code:

print("$MemberName/images/{$imgname[0]}");
if (is_file("$MemberName/images/{$imgname[0]}")) {
print("<a href=\"/members/$MemberName/images/{$imgname[0]}\" target=\"_blank\"><img border=\"1\" src=\"/members/$MemberName/images/{$imgname[0]}\" width=\"70\" height=\"52\"></a>");
}
else {
print("<img src=\"/members/images/blank.gif\" width=\"70\" height=\"52\">");
}

But when I try if (is_file("$MemberName/images/{$imgname[0]}")) { it goes right to the else statement because I assume it's not reading that the file is there.  I've also tried (is_file("members/$MemberName/images/{$imgname[0]}")) { and get the same result.  So I'm lost.  Any insight?

0
 
Job4SuprmnAuthor Commented:
0
 
Job4SuprmnAuthor Commented:
And aib 42, $MemberName is working everywhere, is_file just can't find a file at that location (or any variation of /members/$MemberName/images/ or /$MemberName/images/ or just /images/)
0
 
techtonikConnect With a Mentor Commented:
Of course it can't find it at /images, because you need to call it with relative path. If your code is called from  http://www.bigboosting.com/members/$MemberName/index.php then relative path would be ./images

Just use:
if (is_file("./images/{$imgname[0]}")) {

It should work.
0
 
Job4SuprmnAuthor Commented:
Oh duh!  Thanks techtonik, that worked, can't believe I forgot that.

Appreciate everyone's help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.