...

View Full Version : Passing Variable to function problem



Shaun1978
01-25-2009, 06:52 AM
I'm coding an image website that allows users to upload images, no problem with uploading images. What I am having trouble with is, when a user signs up, I create a specfic folder for that member, this is where there images will go.

The code below is the function, I've obviouslly taken out the ftp login stuff. The function main duty is to change the folder permissions of the members specfic folder, however I am having trouble grabbing the excat members folder.


<?php
function modechanger ($mod,$folder) {
$host = '';
$user = '';
$password = '';


$conn = ftp_connect("$host");

if (!$conn)
{

echo 'Error: Could not connect to ftp server';

exit;
}


@ $result = ftp_login($conn, $user, $password);

if (!$result)
{

echo "Error: Could not log on as $user";

ftp_quit($conn);

exit;
}

if (ftp_site($conn, 'CHMOD '.$mod.' '.$folder)) {

return "true";
}

else {

return "false";

ftp_quit($conn);

exit;
}
ftp_close($conn);
}

Below is code that communicates with above function.



$folder = '../content/$uname/';
modechanger("0777" , "$folder");



I keep getting this msg

"Warning: ftp_site() [function.ftp-site]: ../content/$uname/: No such file or directory in /hsphere/local/home/scotsman/ladyfame.com/stuff/images.php on line 31"

The path to folder is correct and I need to move out of folder and back, then into the content folder. But for some reason the $folder variable isn't passing across with the information. The $uname is set with the members name, this is done through a hidden field in the upload image form.

any help would be greatly appreciated.

:)

oesxyl
01-25-2009, 07:59 AM
few steps:
- echo $folder to see if $uname is expanded( replace ' with " if not)
- check $uname to see if have the value you expect
- of course check if directory exists and the path is correct.

regards

abduraooft
01-25-2009, 10:14 AM
Yes, the variables inside double quoted strings will only get parsed by php.


$folder = '../content/$uname/'; should be

$folder = "../content/$uname/";

Shaun1978
01-26-2009, 06:20 AM
yeah it appears yous are correct regarding the use off " and '.

For some reason the script still isn't working, I'm beginning to think I'll just upload all images to one folder and not bother with member specfic folders. I only looking at doing member folders, as I didn't want to have a clash of excat two image names being uploaded. Think I'll just run a duplicate check on the mysql database side first and make sure there no clashes before uploading, proably be easier.

I've posted full code below, maybe someone here can figure it out. Be handy to know a solution to this if possible but its not essential now.


function modechanger ($mod,$username) {
$host = 'hidden this';
$user = 'hidden this';
$password = 'hidden this';
$folder = "../content/$username/";


$conn = ftp_connect("$host");

if (!$conn)
{

echo 'Error: Could not connect to ftp server';

exit;
}


@ $result = ftp_login($conn, $user, $password);

if (!$result)
{

echo "Error: Could not log on as $user";

ftp_quit($conn);

exit;
}

if (ftp_site($conn, 'CHMOD '.$mod.' '.$folder)) {

return "true";
}

else {

return "false";

ftp_quit($conn);

exit;
}
ftp_close($conn);
}

session_start();
include("hidden this");
switch (@$_GET['do'])
{
case "updimage":
$uname = $_POST['aname'];
modechanger("0777" , "$uname");
$destination1 = "../content/$uname"."/".$_FILES['pix1']['name'];
$temp_file1 = $_FILES['pix1']['tmp_name'];
move_uploaded_file($temp_file1,$destination1);
$filepath = "/images/$uname"."/".$_FILES['pix1']['name'];
modechanger("0755" ,"$uname");
$itit = $_POST['itDesc'];
$lfsect = $_POST['asect'];
$lfcategory = $_POST['cat'];
$cid = date('hisdmy');
$today = date("Y-m-d");
$stat = "waiting";
$connection = mysql_connect($host,$user,$password)
or die ("Couldn't connect to server.");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database.");
$sql = "INSERT INTO lfimages (imageid,loginName,category,section,title,imagepath,createDate,status)
VALUES ('$cid','$uname','$lfcategory','$lfsect','$itit','$filepath','$today','$stat')";
mysql_query($sql);
$mess = "image was succesfully uploaded.";
header("Location: ../panel-images.php?uname=$uname&ltype=$lfsect&message=$mess");
break;

}

I did insert an "echo "$folder"; line underneath where $folder is set within the function. It returned the folder path twice, like ../content/Ladyfame/../content/Ladyfame/.

Ladyfame is the correct members name, no idea why im seeing it twice. I'm also not seeing any error messages, script processes completely (until i put echo folder in). Information is going to the database and the correct path is listed, however the filename isn't attaching to the end of the $filepath variable.

as i said, its not essential to get a soultion to this but would be handy to know one.

:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum