...

View Full Version : chdir ($variable) doesnt work



Ludatha
12-03-2008, 09:07 PM
Hey, I have no idea why this wont work,

chdir($user);
Doesnt work but

chdir('1');
Does work

$user = $session->useridsmall;
Sets $user to 1! Why doesn't it work!




$user = $session->useridsmall;

ob_start();

chdir('upload');


chdir($user);
chdir ('pictures');

// Create image
imagejpeg($realImage, $name, 60);

chdir('thumbnails');

// Create thumb
imagejpeg($thumbImage, $thumbName, 60);


// Output thumb
imagejpeg($thumbImage, NULL, 60);

ob_end_clean();

mlseim
12-03-2008, 09:35 PM
Try seeing what the variable contains ...


<?
$user = $session->useridsmall;
echo $user;
exit;


Also, maybe you should put this as the first line ...

<?
session_start();
$user = $session->useridsmall;
echo $user;
exit;

RichestBoy
12-03-2008, 09:48 PM
yes make sure first that you have proper values in $user.

Always try to debug via echo or print.

Thanks

Ludatha
12-03-2008, 10:04 PM
if I each $user, it prints 1 to the page
The session has already started, I would get an error...
This isn't the whole of my script, just parts of it

eu4ria
12-03-2008, 11:09 PM
I wonder if it is because it is numerical and not a string that is causing the problem.

You could try chdir('"' . $user . '"');

Fou-Lu
12-03-2008, 11:44 PM
I wonder if it is because it is numerical and not a string that is causing the problem.

You could try chdir('"' . $user . '"');

I'm thinking the same thing myself.
Assuming from you're current working directory, if you have another directory listed under the id number (which is a semi-good choice, I'll get to that), chdir will want you to provide it with a string result.
I wouldn't go as overboard as above, I'm too lazy for that :P


$user = (string)$session->useridsmall; // Or use settype


Now, as I was mentioning there is a problem with how you actually use these directories. Its not as big of a deal nowadays, but in the old days it would start acting up after you exceed the numerical value of a short (65535). So, to get around that, we would separate each digit into a path: 65535 = 6/5/5/3/5 with a drill down approach. Worked good. If you're not expecting you're user count to reach or exceed this amount, you're good with just numerical values. Newer OS' shouldn't have a problem with this now either, as I'm about 70% certain it was related to the number of bits the OS operated on.

mlseim
12-04-2008, 03:02 AM
I don't think naming directories by numbers is ever a good idea to begin with.

Fou-Lu
12-04-2008, 03:47 AM
I don't think naming directories by numbers is ever a good idea to begin with.

Yes I totally agree.
Even though numbers are perfectly valid names, they have little to no meaning at all. I mean, what would it be like to get to the hosts file on Windows: C:\1\2\3\4\hosts. Lol, brutal.

When available, use databases to index these, its way faster and you don't have to be tricky to deal with a large volume of users.

Ludatha
12-04-2008, 02:43 PM
Ok, I could change it so the directory names are the username ad not the ID of the user, would that work?

EDIT: I have changed the directory to the username, but it STILL doesn't work! I have echoed the $session->username; and it prints out adam, the directory has been renamed to adam!

I have uploaded the script, you should be able to upload an image by clicking the 'Select Images' button. Try uploading an image and see what happens!
http://ludatha.com/try%202/profile.php?id=adam&act=pictures


<?php
include("include/header.php");
include("include/footer.php");
/* Note: This thumbnail creation script requires the GD PHP Extension.
If GD is not installed correctly PHP does not render this page correctly
and SWFUpload will get "stuck" never calling uploadSuccess or uploadError
*/
error_reporting(E_ALL);
// Get the session Id passed from SWFUpload. We have to do this to work-around the Flash Player Cookie Bug



ini_set("html_errors", "0");
$user = '"' . $session->username . '"';

// Check the upload
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
echo "ERROR:invalid upload";
exit(0);
}
$img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
if (!$img) {
echo "ERROR:could not create image handle ". $_FILES["Filedata"]["tmp_name"];
exit(0);
}

function resizeImage($img,$target_width,$target_height){
$width = imageSX($img);
$height = imageSY($img);

if (!$width || !$height) {
echo "ERROR:Invalid width or height";
exit(0);
}
$target_ratio = $target_width / $target_height;

$img_ratio = $width / $height;


if ($target_ratio > $img_ratio) {
$new_height = $target_height;
$new_width = $img_ratio * $target_height;
} else {
$new_height = $target_width / $img_ratio;
$new_width = $target_width;
}

if ($new_height > $target_height) {
$new_height = $target_height;
}
if ($new_width > $target_width) {
$new_height = $target_width;
}

$new_img = ImageCreateTrueColor($target_width, $target_height);
if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, 0)) { // Fill the image black
echo "ERROR:Could not fill new image";
exit(0);
}

if (!@imagecopyresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0, 0, $new_width, $new_height, $width, $height)) {
echo "ERROR:Could not resize image";
exit(0);
}

return $new_img;
}



// Build the thumbnail
$thumbImage = resizeImage($img,100,100);
$realImage = resizeImage($img,800,600);

if (!isset($_SESSION["file_info"])) {
$_SESSION["file_info"] = array();
}

$id = md5(100 + rand(1, 10));
$thumbName = 'thumb_'. $id . '.jpg';
$name = 'pic_'. $id . '.jpg';

$code = rand(1, 999999999);

mysql_connect('localhost', 'root', '');
mysql_select_db('ludatha_base');

$sql = "INSERT INTO images (user, file, thumb, folder, code) VALUES ('$user', '$name', '$thumbName', 'root', '$code')";

$result = mysql_query($sql);


// Use a output buffering to load the image into a variable
ob_start();

chdir('upload');


chdir($user);
chdir ('pictures');

// Create image
imagejpeg($realImage, $name, 60);

chdir('thumbnails');

// Create thumb
imagejpeg($thumbImage, $thumbName, 60);


// Output thumb
imagejpeg($thumbImage, NULL, 60);

ob_end_clean();



echo "FILEID:" . $code; // Return the file id to the script

?>

That is the whole script, not including the session class which is started in the header.

If I move the chdir outside the output buffering I get this error:

Warning: chdir(): No error (errno 0) in D:\wamp\www\try 2\upload.php on line 16

Line 16 is:
chdir($user);

mlseim
12-04-2008, 04:59 PM
How is chdir($user) on line 16?


am I figuring out the error message wrong?

Ludatha
12-04-2008, 05:01 PM
How is chdir($user) on line 16?


am I figuring out the error message wrong?

I said:
If I move the chdir outside the output buffering I get this error:

Warning: chdir(): No error (errno 0) in D:\wamp\www\try 2\upload.php on line 16

I moved it to line 16

-----------------
After much fiddling I've got it work ALMOST work...
chdir now works, but $session->username seems to be returning "Guest" instead of the actual logged in user, in all the other scripts it works fine...
The ONLY different between this script and others is that it is called by javascript and not through the browser
-----------------

mlseim
12-04-2008, 08:01 PM
I wonder what would happen if you saved the $user as a cookie instead
of a SESSION. Cookies can be accessed by both Javascript and PHP.

Ludatha
12-04-2008, 08:07 PM
I suppose it would work but that would make it insecure, cookies can be changed client side.

abduraooft
12-04-2008, 08:14 PM
The ONLY different between this script and others is that it is called by javascript and not through the browserCan we see your relevant code?


I suppose it would work but that would make it insecure, cookies can be changed client side. javascript is also not secure either.

Ludatha
12-04-2008, 08:39 PM
Im using something called SWFUpload, legend code :)
http://swfupload.org/

Ok, well on the actual page where it is called:

var swfu;
window.onload = function () {
swfu = new SWFUpload({
// Backend Settings
upload_url: "upload.php", // Relative to the SWF file or absolute
post_params: {"PHPSESSID": "<?php echo session_id() ?>'"},

// File Upload Settings
file_size_limit : "2 MB", // 2MB
file_types : "*.jpg",
file_types_description : "JPG Images",
file_upload_limit : "0",

// Event Handler Settings - these functions as defined in Handlers.js
// The handlers are not part of SWFUpload but are part of my website and control how
// my website reacts to the SWFUpload events.
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,
upload_progress_handler : uploadProgress,
upload_error_handler : uploadError,
upload_success_handler : uploadSuccess,
upload_complete_handler : uploadComplete,

// Button Settings
button_image_url : "images/SmallSpyGlassWithTransperancy_17x18.png", // Relative to the SWF file
button_placeholder_id : "spanButtonPlaceholder",
button_width: 180,
button_height: 18,
button_text : '<span class="button">Select Images <span class="buttonSmall">(2 MB Max)</span></span>',
button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; } .buttonSmall { font-size: 10pt; }',
button_text_top_padding: 2,
button_text_left_padding: 18,
button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
button_cursor: SWFUpload.CURSOR.HAND,

// Flash Settings
flash_url : "swfupload.swf",

custom_settings : {
upload_target : "divFileProgressContainer"
},

// Debug Settings
debug: false
});
};

That code is present in the header.

Handlers.js, is very long but:

function fileQueueError(file, errorCode, message) {
try {
var imageName = "error.gif";
var errorName = "";
if (errorCode === SWFUpload.errorCode_QUEUE_LIMIT_EXCEEDED) {
errorName = "You have attempted to queue too many files.";
}

if (errorName !== "") {
alert(errorName);
return;
}

switch (errorCode) {
case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
imageName = "zerobyte.gif";
break;
case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
imageName = "toobig.gif";
break;
case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
default:
alert(message);
break;
}

addImage("images/" + imageName);

} catch (ex) {
this.debug(ex);
}

}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
try {
if (numFilesQueued > 0) {
this.startUpload();
}
} catch (ex) {
this.debug(ex);
}
}

function uploadProgress(file, bytesLoaded) {

try {
var percent = Math.ceil((bytesLoaded / file.size) * 100);

var progress = new FileProgress(file, this.customSettings.upload_target);
progress.setProgress(percent);
if (percent === 100) {
progress.setStatus("Creating thumbnail...");
progress.toggleCancel(false, this);
} else {
progress.setStatus("Uploading...");
progress.toggleCancel(true, this);
}
} catch (ex) {
this.debug(ex);
}
}

function uploadSuccess(file, serverData) {
try {
var progress = new FileProgress(file, this.customSettings.upload_target);

if (serverData.substring(0, 7) === "FILEID:") {
addImage("thumbnail.php?id=" + serverData.substring(7) + "&type=thumb", "thumbnail.php?id=" + serverData.substring(7) + "&type=pic");
progress.setStatus("Thumbnail Created.");
progress.toggleCancel(false);
} else {
addImage("images/error.gif");
progress.setStatus("Error.");
progress.toggleCancel(false);
alert(serverData);

}


} catch (ex) {
this.debug(ex);
}
}

function uploadComplete(file) {
try {
/* I want the next upload to continue automatically so I'll call startUpload here */
if (this.getStats().files_queued > 0) {
this.startUpload();
} else {
var progress = new FileProgress(file, this.customSettings.upload_target);
progress.setComplete();
progress.setStatus("All images received.");
progress.toggleCancel(false);
}
} catch (ex) {
this.debug(ex);
}
}

function uploadError(file, errorCode, message) {
var imageName = "error.gif";
var progress;
try {
switch (errorCode) {
case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
try {
progress = new FileProgress(file, this.customSettings.upload_target);
progress.setCancelled();
progress.setStatus("Cancelled");
progress.toggleCancel(false);
}
catch (ex1) {
this.debug(ex1);
}
break;
case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
try {
progress = new FileProgress(file, this.customSettings.upload_target);
progress.setCancelled();
progress.setStatus("Stopped");
progress.toggleCancel(true);
}
catch (ex2) {
this.debug(ex2);
}
case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
imageName = "uploadlimit.gif";
break;
default:
alert(message);
break;
}

addImage("images/" + imageName);

} catch (ex3) {
this.debug(ex3);
}

}

function addImage(src, largesrc) {
var aTag = document.createElement("a");
var newImg = document.createElement("img");

aTag.href = largesrc;
aTag.rel = "lightbox";
newImg.style.margin = "5px";
aTag.appendChild(newImg);



document.getElementById("thumbnails").appendChild(aTag);
if (newImg.filters) {
try {
newImg.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0;
} catch (e) {
// If it is not set initially, the browser will throw an error. This will set it if it is not set yet.
newImg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + 0 + ')';
}
} else {
newImg.style.opacity = 0;
}

newImg.onload = function () {
fadeIn(newImg, 0);
};
newImg.src = src;
}

function fadeIn(element, opacity) {
var reduceOpacityBy = 5;
var rate = 30; // 15 fps


if (opacity < 100) {
opacity += reduceOpacityBy;
if (opacity > 100) {
opacity = 100;
}

if (element.filters) {
try {
element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity;
} catch (e) {
// If it is not set initially, the browser will throw an error. This will set it if it is not set yet.
element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
}
} else {
element.style.opacity = opacity / 100;
}
}

if (opacity < 100) {
setTimeout(function () {
fadeIn(element, opacity);
}, rate);
}
}



/* ******************************************
* FileProgress Object
* Control object for displaying file info
* ****************************************** */

function FileProgress(file, targetID) {
this.fileProgressID = "divFileProgress";

this.fileProgressWrapper = document.getElementById(this.fileProgressID);
if (!this.fileProgressWrapper) {
this.fileProgressWrapper = document.createElement("div");
this.fileProgressWrapper.className = "progressWrapper";
this.fileProgressWrapper.id = this.fileProgressID;

this.fileProgressElement = document.createElement("div");
this.fileProgressElement.className = "progressContainer";

var progressCancel = document.createElement("a");
progressCancel.className = "progressCancel";
progressCancel.href = "#";
progressCancel.style.visibility = "hidden";
progressCancel.appendChild(document.createTextNode(" "));

var progressText = document.createElement("div");
progressText.className = "progressName";
progressText.appendChild(document.createTextNode(file.name));

var progressBar = document.createElement("div");
progressBar.className = "progressBarInProgress";

var progressStatus = document.createElement("div");
progressStatus.className = "progressBarStatus";
progressStatus.innerHTML = "&nbsp;";

this.fileProgressElement.appendChild(progressCancel);
this.fileProgressElement.appendChild(progressText);
this.fileProgressElement.appendChild(progressStatus);
this.fileProgressElement.appendChild(progressBar);

this.fileProgressWrapper.appendChild(this.fileProgressElement);

document.getElementById(targetID).appendChild(this.fileProgressWrapper);
fadeIn(this.fileProgressWrapper, 0);

} else {
this.fileProgressElement = this.fileProgressWrapper.firstChild;
this.fileProgressElement.childNodes[1].firstChild.nodeValue = file.name;
}

this.height = this.fileProgressWrapper.offsetHeight;

}
FileProgress.prototype.setProgress = function (percentage) {
this.fileProgressElement.className = "progressContainer green";
this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
this.fileProgressElement.childNodes[3].style.width = percentage + "%";
};
FileProgress.prototype.setComplete = function () {
this.fileProgressElement.className = "progressContainer blue";
this.fileProgressElement.childNodes[3].className = "progressBarComplete";
this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setError = function () {
this.fileProgressElement.className = "progressContainer red";
this.fileProgressElement.childNodes[3].className = "progressBarError";
this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setCancelled = function () {
this.fileProgressElement.className = "progressContainer";
this.fileProgressElement.childNodes[3].className = "progressBarError";
this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setStatus = function (status) {
this.fileProgressElement.childNodes[2].innerHTML = status;
};

FileProgress.prototype.toggleCancel = function (show, swfuploadInstance) {
this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
if (swfuploadInstance) {
var fileID = this.fileProgressID;
this.fileProgressElement.childNodes[0].onclick = function () {
swfuploadInstance.cancelUpload(fileID);
return false;
};
}
};


There is also the prototype framework installed

marli
01-14-2009, 12:46 AM
ok...try trim($variable)
i neally killed my self with this one!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
you have a \n or \t or somthing in the string.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum