Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts

    chdir ($variable) doesnt work

    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!



    PHP Code:
    $user $session->useridsmall;

    ob_start();
        
        
    chdir('upload');
        
        
        
    chdir($user);
        
    chdir ('pictures');
        
        
    // Create image
        
    imagejpeg($realImage$name60);
        
        
    chdir('thumbnails');
        
        
    // Create thumb
        
    imagejpeg($thumbImage$thumbName60);
        

        
    // Output thumb
        
    imagejpeg($thumbImageNULL60);

        
    ob_end_clean(); 

  • #2
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,387
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    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;

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    yes make sure first that you have proper values in $user.

    Always try to debug via echo or print.

    Thanks

  • #4
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts
    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

  • #5
    New Coder
    Join Date
    May 2006
    Posts
    81
    Thanks
    0
    Thanked 11 Times in 11 Posts
    I wonder if it is because it is numerical and not a string that is causing the problem.

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

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by eu4ria View Post
    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
    PHP Code:
    $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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,387
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    I don't think naming directories by numbers is ever a good idea to begin with.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by mlseim View Post
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts
    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.p...m&act=pictures


    PHP Code:
    <?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_img00$target_width-1$target_height-10)) {    // 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)/200$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(110));   
        
    $thumbName 'thumb_'$id '.jpg';
        
    $name 'pic_'$id '.jpg'
        
        
    $code rand(1999999999);
        
        
    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$name60);
        
        
    chdir('thumbnails');
        
        
    // Create thumb
        
    imagejpeg($thumbImage$thumbName60);
        

        
    // Output thumb
        
    imagejpeg($thumbImageNULL60);

        
    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);
    Last edited by Ludatha; 12-04-2008 at 02:50 PM.

  • #10
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,387
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    How is chdir($user) on line 16?


    am I figuring out the error message wrong?

  • #11
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by mlseim View Post
    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

    -----------------
    Last edited by Ludatha; 12-04-2008 at 06:47 PM.

  • #12
    Master Coder mlseim's Avatar
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,387
    Thanks
    8
    Thanked 1,077 Times in 1,068 Posts
    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.

  • #13
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts
    I suppose it would work but that would make it insecure, cookies can be changed client side.

  • #14
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    The ONLY different between this script and others is that it is called by javascript and not through the browser
    Can we see your relevant code?

    Quote Originally Posted by Ludatha View Post
    I suppose it would work but that would make it insecure, cookies can be changed client side.
    javascript is also not secure either.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #15
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts
    Im using something called SWFUpload, legend code
    http://swfupload.org/

    Ok, well on the actual page where it is called:
    Code:
    	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:
    Code:
    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
    Last edited by Ludatha; 12-04-2008 at 07:41 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •